Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reg Ex Search c#

Posted on 2011-02-14
3
Medium Priority
?
355 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:Charles Baldo
  • 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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…
Suggested Courses

927 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