regEx for pulling out google links

i want to use code like this to pull all 10 links per Google result page. i am doing this in a c# web app but it may turn into console or desktop app.


i am getting the content like this:
WebClient w = new WebClient();
string pageSource = w.DownloadString(http://www.google.com/search?hl=en&q=weather&start=0)

then i wanted to be able to use regEx to extract all top level url links.

any ideas? or ideas on how to retrieve urls from Google results?

thoughts?
supergirl2008Asked:
Who is Participating?
 
shadow77Connect With a Mentor Commented:
This should do what you want.  If you display more than 10 results per page, it will find the additional links.
private void test14()
{
    WebClient w = new WebClient();
    string pageSource = w.DownloadString("http://www.google.com/search?hl=en&q=weather&start=0");
    string s = "(?<=<h3 class=r><a href=\")([^\"]+)";
    Regex re = new Regex(s);
    MatchCollection m = re.Matches(pageSource);
    for (int i = 0; i < m.Count; i++)
    {
        Debug.Print("Match result {0} is \"{1}\".", i, m[i]);
    }
}

Open in new window

0
 
SriVaddadiCommented:
WebClient w = new WebClient();
            string pageSource = w.DownloadString("http://www.google.com/search?hl=en&q=weather&start=0");
            Regex reg = new Regex("<a[^>]*>.*?</a>");
            StringBuilder sb = new StringBuilder();
            foreach(Match m in reg.Matches(pageSource))
            {
                sb.AppendLine(m.Value);
            }
0
 
Alfred A.Commented:
Check this out to get an idea.

http://stackoverflow.com/questions/369147/javascript-regex-to-extract-anchor-text-and-url-from-anchor-tags

http://www.askaboutphp.com/25/regex-extract-content.html

The links above are in javascript and PHP but you can use the pattern/s to incorporate in a System.Text.RegularExpressions.Regex function in C#.


0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
supergirl2008Author Commented:
shadow77:how can i learn to be a super regex master like you?
0
 
supergirl2008Author Commented:
shadow77:how can i learn to be a super regex master like you?
0
 
shadow77Commented:
Thanks for the kind words.  Jan Goyvaerts, who really is a super regex master, has an excellent free web site full of great info and tutorials (http://www.regular-expressions.info/).  He also has an excellent product, Regex Buddy ($39.95, http://www.regular-expressions.info/regexbuddy.html), that lets you test expressions.  It explains any RegEx as you build it and provides test tools and it supports several RegEx dialects (.NET, JavaScript, Perl, etc)

Regarding the specific problem you posed originally, I looked through the HTML to see what distinguished the links you wanted from all the others (in this case,

).  The (?<=...) subexpression excludes any matches that do not follow the pattern that replaces ...  It's called a lookbehind; there's also lookahead; and both can be positive (subexpression must appear) or negative (subexpression must not appear).

There are also lots of good people who post in the Regular Expressions zone on this site.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.