2012-06-11 62 views
0

我有一些HTML,我需要解析(在一個大文件),文本,和我很感興趣,看起來像這樣的部分:正則表達式不匹配在C#中的字符串

... 
<div id="whatever" class="whatever whatever">some title with <em>html</em> and other such tags in it, but never a div tag</div> 
... 

現在我想用HTML解決DIV中的文本。下面是我對正則表達式(使用組):

<div id=\"whatever\" class=\"whatever whatever\">(?<title>[^</div>]*?)</div> 

這樣的想法存在,我會匹配整個事情,並得到一組的所有文字最多的地步</div>發生(因爲字符串末尾沒有其他標識因素)。

^in []不起作用,因爲它是這些字符中的「任何」,而不是我想要的字符串「</div>」。任何想法如何使這項工作?

+6

只是不這樣做。改爲使用HTML解析器(如HtmlAgilityPack)。重複:[RegEx匹配除XHTML獨立標籤以外的開放標籤](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – spender

回答

0
Match m=Regex.Match(s,"\\<div id=\"whatever\" class=\"whatever whatever\">(.*?)\\<\\/div\\>");              
Console.WriteLine(m.Groups[1].Value); 
相關問題