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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
shadow77Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.