Solved

Reg Ex Search c#

Posted on 2011-02-14
3
341 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

830 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