Solved

Reg Ex Search c#

Posted on 2011-02-14
3
340 Views
Last Modified: 2012-05-11
Hi

I am new to regular expressions.  I am doing search in text of HTML. What I want to do is find a  tag then from that point find the next "</div>" tag

I have this

            String body = sb.ToString();
            String Start = "<div><a class=\"resultName\"";
            String Stop = "</div>";

            Match m;
            m = new Regex(Start, RegexOptions.Singleline | RegexOptions.IgnoreCase).Match(body);
            int i = m.Index;

now  find from i how many characters away is Stop "</div>"

so instead of  somethikng like this

            txtList.Text = body.Substring(i, 200);

But what I would like to do is txtList.Text = body.Substring(i, x);

Where x is the number of characters to the next "</div>"

Hope that was expained well

Thanks


0
Comment
Question by:charlesbaldo
  • 2
3 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 34894268
Can't u use bodymsubstring(i,body.indexOf("<div>"))
0
 
LVL 20

Expert Comment

by:informaniac
ID: 34894299
Sry shd be body.Substring....
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34894827
Try something like this:
String body = sb.ToString();
String Start = "<div><a class=\"resultName\"";
String Stop = "</div>";

// Here, .*? means find zero-or-more ( * ) of any
//  character ( . ), but find only the minimum number
//  that will satisfy a valid match ( ? )
String pattern = string.Concat(Start, "(.*?)", Stop);

Match m;
m = Regex.Match(body, pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
int i = m.Index + m.Length;

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now