[英]How to match these strings with Regex?
<div>
<a href="http://website/forum/f80/ThreadLink-new/" id="thread_gotonew_565407"><img class="inlineimg" src="http://website/forum/images/buttons/firstnew.gif" alt="Go to first new post" border="0" /></a>
[MULTI]
<a href="http://website/forum/f80/ThreadLink/" id="thread_title_565407" style="font-weight:bold">THREAD TITLE</a>
</div>
我知道我感興趣的鏈接將變為粗體:
font-weight:bold
但是鏈接本身位於前面。 如何匹配兩個鏈接地址:
http://website/forum/f80/ThreadLink/
和線程標題:
THREAD TITLE
編輯:Internet Explorer HTML代碼是非常不同的:
<A style="FONT-WEIGHT: bold" id=thread_title_565714
href="http://LinkAddress-565714/">ThreadTitle</A> </DIV>
.*<a href="(.*?)".*style="font-weight:bold">(.*?)</a>
比賽組1:網址比賽組2:線程標題
這將匹配任何粗體鏈接。 如果要匹配特定值,請用這些值替換(。*?)。
嘗試這個:
線程標題
<A style="FONT-WEIGHT: bold" id=(?<id>.*?)[\s\S]*? href="(?<url>.*?)">(?<title>.*?)</A>
因此,您可以使用:
Regex link = new Regex(@"<A style=""FONT-WEIGHT: bold"" id=(?<id>.*?)[\s\S]*? href=""(?<url>.*?)"">(?<title>.*?)</A>");
foreach (Match match in link.Matches(input))
{
Console.WriteLine(
"Id={0}, Url={1}, Title={2}",
match.Groups["id"].Value,
match.Groups["url"].Value,
match.Groups["title"].Value);
}
<a href="([^"]*)"[^>]*style="[^"]*font-weight:bold[^"]*"[^>]*>([^<]*)</a>
與前一個答案基本相同,除了我用[^"]*
等替換了它們的.*
。在第一個匹配項中,這防止它與下一個雙引號符號之外的任何內容匹配。如果不這樣做,則可以在輸入看起來像這樣的情況下,匹配得太多了:
<a href="#dont_match_me">Don't match me</a><br/>
<a href="http://website/forum/f80/ThreadLink/ style="font-weight:bold">THREAD TITLE</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.