-1

I'm trying to extract a url from a string.

{ns:"images",k:"5127",mid:"A04F21EB77CF61E10E43BA33CF1986CA44357448"
,md5:"e2987d19c953bd836ec8fd2e0aa8492",surl:"http://someURLIdontwant/"
,imgurl:"http://THISISTHEURLINEED.jpg",tid:"OIP.Me2987d199c953bd836ec8fd2e0aa8492H0"
,ow:"300", docid:"608010036892077154",oh:"225",tft:"49"}

So it is located after "imgurl:". I am no expert on Regex and all I could produce is:

imgurl:'(.*)',tid

whitch worked on some online regex tester. But not the way I'm using it in C# apperantly.

webClient.DownloadFile(System.Text.RegularExpressions.Regex.Match
(stringWithText, "imgurl:'(.*)',tid").Groups[1].Value,"path\file.jpg");

Can it be done? Thanks

Jomasdf
  • 258
  • 2
  • 13
  • 3
    You have double quotes in your string, and single apostrophes in the pattern. – Wiktor Stribiżew May 30 '16 at 20:13
  • 6
    That data is JSON. Use a Json Parser to deserialize it an access `imgurl` directly. See [How to parse JSON in C#](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Manfred Radlwimmer May 30 '16 at 20:13
  • Parsing nested structures (HTML/XML/JSON) with regex is at best very hard - please check out http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ for guidance and following search https://www.bing.com/search?q=c%23+parse+json+regex for more similar answers. – Alexei Levenkov May 30 '16 at 20:21

1 Answers1

1

As @WiktorStribiżew already pointed out: The expression is almost correct. Use this instead:

Regex.Match(stringWithText, "imgurl:\"(.*)\",tid").Groups[1].Value

Example on dotNetFiddle

And as I mentioned earlier in a comment: You should parse the Json data instead.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62