Get an inner-text from a regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Spippo
    New Member
    • Nov 2007
    • 15

    Get an inner-text from a regular expression

    Hello

    Is it possible to retrieve then inner-text from a link with regular expressions?
    For example, I can retrieve all links with regular expressions. With:

    Code:
    string pattern = @"<a href="".*"">.*</a>";
    MatchCollection mc = Regex.Matches(webBrowser1.Document.Body.InnerHtml, pattern);
    
    foreach(object obj in mc)
    {
        Console.WriteLine(obj.ToString());
    }
    So i get the following matches:

    Code:
    <a href="http://www.thescripts.com">The Scripts</a>
    <a href="http://www.mysite.com">My Site</a>
    <a href="http://www.anothersite.com">Yet another site</a>
    But is it also possible to retrieve only the innertexts? So I would get the following results:

    Code:
    The Scripts
    My Site
    Yet another site
    Or even better, more innertexts in one item like:
    Code:
    http://www.thescripts.com The Scripts
    http://www.mysite.com My Site
    http://www.anothersite.com Yet another site
    Thanks
  • PareshRathod
    New Member
    • Mar 2007
    • 28

    #2
    Hi Guys,

    Nice to see that somebody needs my Regular Expression Talent...
    Try This,
    This will solve your problem:

    string pattern = @"(\<a href="")(.*?)(" "\>)(.*?)\</a\>";
    MatchCollection match=Regex.Mat ches(webBrowser 1.Document.Body .InnerHtml, pattern,RegexOp tions.IgnoreCas e);
    foreach (Match obj in match)
    {
    Console.WriteLi ne(obj.Groups[2].Value.ToString () + " => " + obj.Groups[4].Value.ToString ());
    }

    Notes:
    1. () define groups in search pattern.
    2. RegexOptions.Ig noreCase implements case insensitive search.
    3. object -> Match in foreach to retrieve groups.

    Say thanks dear if you like it, it shows our goodness.

    Paresh

    Originally posted by Spippo
    Hello

    Is it possible to retrieve then inner-text from a link with regular expressions?
    For example, I can retrieve all links with regular expressions. With:

    Code:
    string pattern = @"<a href="".*"">.*</a>";
    MatchCollection mc = Regex.Matches(webBrowser1.Document.Body.InnerHtml, pattern);
    
    foreach(object obj in mc)
    {
        Console.WriteLine(obj.ToString());
    }
    So i get the following matches:

    Code:
    <a href="http://www.thescripts.com">The Scripts</a>
    <a href="http://www.mysite.com">My Site</a>
    <a href="http://www.anothersite.com">Yet another site</a>
    But is it also possible to retrieve only the innertexts? So I would get the following results:

    Code:
    The Scripts
    My Site
    Yet another site
    Or even better, more innertexts in one item like:
    Code:
    http://www.thescripts.com The Scripts
    http://www.mysite.com My Site
    http://www.anothersite.com Yet another site
    Thanks

    Comment

    • Spippo
      New Member
      • Nov 2007
      • 15

      #3
      OMG, Thanks a lot.
      I will do as you requested:

      Thanks dear!!!!!!!!

      You are my god

      Comment

      • PareshRathod
        New Member
        • Mar 2007
        • 28

        #4
        Hi Guy,

        No need to put me in that much big category...
        Just a simple thanks can work.... :)

        Have a nice time..
        Paresh

        Originally posted by Spippo
        OMG, Thanks a lot.
        I will do as you requested:

        Thanks dear!!!!!!!!

        You are my god

        Comment

        Working...