简体   繁体   中英

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> 

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM