這裏是我的正則表達式:正則表達式不工作的C#
href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))
,這裏是我有:
"<p>dfhdfh</p>\r\n<p><a href=\"/Content/blabla/345/344\">najnov</a></p>\r\n<p> </p>\r\n<p><a href=\"/Content/blabla/345/323:test 1\">test 1 </a></p>"
但m.Groups是:
{href="/Content/blabla/345/344"}
{/Content/blabla/345/344}
如何獲得第二個href在m?
這裏是我的代碼:
Match m = Regex.Match(myString, "href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase);
if (m.Success)
{
for (int ij = 0; ij < m.Groups.Count; ij++)
myString = myString.Replace(m.Groups[ij].Value.Substring(7), m.Groups[ij].Value.Substring(m.Groups[ij].Value.LastIndexOf("/") + 1));
}
[您不應該試圖用正則表達式解析HTML。](http://stackoverflow.com/a/1732454/41071)改爲使用HTML解析器,比如HTML Agility Pack。 – svick 2012-04-20 09:47:34
另外,你能告訴我們你的代碼實際上使用你的正則表達式嗎? – svick 2012-04-20 09:48:52
@svick有它。 – 2012-04-20 09:53:07