-1
我想從字符串中提取一個url。在C#中使用正則表達式字符串#
{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"}
所以它位於「imgurl:」之後。我不是正則表達式的專家,我可以生產的所有產品是:
imgurl:'(.*)',tid
在一些在線正則表達式測試中工作。但不是我在C#中使用它的方式。
webClient.DownloadFile(System.Text.RegularExpressions.Regex.Match
(stringWithText, "imgurl:'(.*)',tid").Groups[1].Value,"path\file.jpg");
可以這樣做嗎?謝謝
您的字符串中有雙引號,模式中有單引號。 –
該數據是JSON。使用Json解析器直接對它進行反序列化,直接訪問'imgurl'。請參閱[如何在C#中解析JSON](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) –
使用正則表達式解析嵌套結構(HTML/XML/JSON)是最好非常努力 - 請檢查http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/的指導和以下搜索https://www.bing .com/search?q = c%23 + parse + json + regex獲取更多相似的答案。 –