Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi. I can't get my RegEx results. So here's a little code snipet what I have made. This code is exact the same like in my not working project. :)

Code:

   string url = string.Format("http://www.google.lv/search?hl=lv&q=roze%23&bav=on.2,or.r_gc.r_pw.&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi&sout=1&biw=1280&bih=651");
   WebClient wb = new WebClient();
   string content = wb.DownloadString(url);

   Regex reg = new Regex(@"imgres\?imgurl.*?(,""){5}");

   MatchCollection mm = reg.Matches(content);

   foreach (Match item in mm)
   {
       Console.WriteLine(item.ToString());
   }
Console.ReadLine();

I download google image page content. I use RegEx to get some string for my future activitys. But it just don't work. This reg I build in "RegExBuilder" app. and it showes me 21 match.
In VS 2010 -> No match! :)
So can anyone tell me -> Why it doesn't work?
Thanks!

P.S. In my time zone it is 1:20. I will answer in morning :)
Posted

1 solution

C#
//Get related text block
string subContent = Regex.Match(content,@"(?<=dyn.setResults\(\[).*?(?=\]\);)").Value;
// Get rid of empty lists
subContent = subContent.Replace("[],", "");
// Get list of images
MatchCollection mc = Regex.Matches(subContent , @"(?<=\[).*?(?=\])");
// Now mc holds your 21 results
foreach(System.Text.RegularExpressions.Match m in mc)
{
    // Split parameters of dyn.setResults
    string[] args = m.Value.Split(',');
    // 4th value of args hold your image url
}

Quite dirty but, hope it helps.
 
Share this answer
 
v2
Comments
valza 31-May-11 1:15am    
Yes, it works.
Can you give me a short comment. Why my code didn't work? What was wrong? :)
yesotaso 31-May-11 4:40am    
Well, the logic behind your pattern is a little bit insufficient. I dont know how it seemed to work in the first place. As you can see I did not search for "imgres?imgurl", yes that is close to result but if you dont pinpoint exact location of goodies in someoneelse's string you'd waste quite a bit of time searching your way. So the brackets '[' ']' are the key to pinpoint target you should have looked for those.
valza 31-May-11 5:37am    
Yes, thanks! before you post answer I did some work, to understand your code.

Again Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900