我有一些網站源流我想解析。我目前的正則表達式是這樣的:簡單的正則表達式幫助使用C#(包括正則表達式模式)
Regex pattern = new Regex (
@"<a\b # Begin start tag
[^>]+? # Lazily consume up to id attribute
id\s*=\s*['""]?thread_title_([^>\s'""]+)['""]? # $1: id
[^>]+? # Lazily consume up to href attribute
href\s*=\s*['""]?([^>\s'""]+)['""]? # $2: href
[^>]* # Consume up to end of open tag
> # End start tag
(.*?) # $3: name
</a\s*> # Closing tag",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
但它不再匹配鏈接。我包括一個樣本字符串here。
基本上我試圖匹配這些:
<a href="http://visitingspain.com/forum/f89/how-to-get-a-travel-visa-3046631/" id="thread_title_3046631">How to Get a Travel Visa</a>
"http://visitingspain.com/forum/f89/how-to-get-a-travel-visa-3046631/" is the **Link**
304663` is the **TopicId**
"How to Get a Travel Visa" is the **Title**
在我張貼的樣本中,有至少3個,我沒算其他的。
此外,我使用RegexHero(在線和免費)在添加到代碼之前交互式地查看我的匹配。
使用HtmlAgilityPack。 – SLaks
@Joan Venge作爲參考:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – 2011-09-25 04:40:51
感謝pst,還沒有見過那一個。 –