开发者

How to match these strings with Regex?

开发者 https://www.devze.com 2022-12-09 12:07 出处:网络
<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 f
<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> 

I know for a fact that the link I am interested in is gonna be bold:

font-weight:bold

But the link itself comes before. How would I be able to match both the link address:

http://website/forum/f80/ThreadLink/

and the thread title:

THREAD TITLE

EDIT: Internet Explorer HTML code开发者_运维问答 is very different:

  <A style="FONT-WEIGHT: bold" id=thread_title_565714 
      href="http://LinkAddress-565714/">ThreadTitle</A> </DIV>


.*<a href="(.*?)".*style="font-weight:bold">(.*?)</a>

Match group 1: Url Match group 2: Thread Title

This will match any bold link. If you want to match a particular one, replace the (.*?) with those values.


Try this:

ThreadTitle

<A style="FONT-WEIGHT: bold" id=(?<id>.*?)[\s\S]*? href="(?<url>.*?)">(?<title>.*?)</A>

So you can use:

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>

Much the same as the previous answers, except I've replaced their .* with [^"]* etc. In the first match, this prevents it from matching anything outside the next double-quote symbol. Without doing this, if you could match too much in cases where the input looked like this:

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消