Solved

Reg Ex Search c#

Posted on 2011-02-14
3
339 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
Comment Utility
Can't u use bodymsubstring(i,body.indexOf("<div>"))
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
Sry shd be body.Substring....
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is MicroStrategy.NET? 2 43
Using Simpleject with Class Library 11 24
Regex code,how to do this? 3 20
Create XML 5 30
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

762 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

8 Experts available now in Live!

Get 1:1 Help Now