开发者

Simple line matching using Regex

开发者 https://www.devze.com 2023-02-18 01:49 出处:网络
I have this string stream: \"do=whoposted&amp;t=1934067\" rel=nofollow>61</A></TD><TD class=alt2 align=middle>5,286</TD></TR><TR><TD id=td_threadstatusic

I have this string stream:

"do=whoposted&amp;t=1934067" rel=nofollow>61</A></TD><TD class=alt2 align=middle>5,286</TD></TR><TR><TD id=td_threadstatusicon_1911046 class=alt1><开发者_运维知识库;IMG id=thread_statusicon_1911046 border=0 alt="" src="http://url.com/forum/images/statusicon/thread_new.gif"> </TD><TD class=alt2><IMG title=Node border=0 alt=Node src="http://url.com/forum/images/icons/new.png"></TD><TD id=td_threadtitle_1911046 class=alt1 title="http://lulzimg.com/i14/7bd11b.jpg &#10; &#10;Complete name : cool-thread...."><DIV><A id=thread_gotonew_1911046 href="http://url.com/forum/f80/cool-topic-new/"><IMG class=inlineimg title="Go to first new post" border=0 alt="Go to first new post" src="http://url.com/forum/images/buttons/firstnew.gif"></A> [MULTI] <A style="FONT-WEIGHT: bold" id=thread_title_1911046 href="http://url.com/forum/f80/cool-topic-name-1911046/">Cool Topic Name</A> </DIV><DIV class=smallfont><SPAN style="CURSOR: pointer" onclick="window.open('http://url.com/forum/members/u2031889/', '_self')">m3no</SPAN> </DIV></TD><TD class=alt2 title="Replies: 11, Views: 1,554"><DIV style="TEXT-ALIGN: right; WHITE-SPACE: nowrap" class=smallfont>Today <SPAN class=time>08:04 AM</SPAN><BR>by <A href="http://url.com/forum/members/u1131830/" rel=nofollow>karetsos</A> <A "

The lines I am interested are similar to this:

<A style="FONT-WEIGHT: bold" id=thread_title_1911046 href="http://url.com/forum/f80/cool-topic-name-1911046/">Cool Topic Name</A>

From here all I am trying to extract are:

Thread id: 1911046 (could be from either location in the string)
Thread name: "Cool Topic Name"
Thread link: "http://url.com/forum/f80/cool-topic-name-1911046/"

Currently I use this:

Regex pattern = new Regex ( "<A\\s+href=\"([^\"]*)\">([^\\x00]*?)\\s+id=thread_title_(\\S+)</A>" );

MatchCollection matches = pattern.Matches ( doc.ToString ( ) );

foreach ( Match match in matches )
{
    int id = Convert.ToInt32 ( match.Groups [ 1 ].Value );

    string name = match.Groups [ 3 ].Value;
    string link = match.Groups [ 2 ].Value;

    ...
}

I would appreciate if someone can help me fix the pattern to match it. This used to work but it returns 0 matches.


Michael Papile's answer works. Remove the forward slashes (/) from the beginning and end of the pattern that you showed in your last comment. Forward slashes are pattern delimiters in Ruby - we don't use them in .NET:

var rg = new Regex(@"<A(?:[^<]*)thread_title_(\d+) href=""([^""]*)"">([^<]*)");

(In an verbatim string (@"...") you only need to escape double quotes by doubling them up.

Edit: corrected pattern added by Richard to use the latest version from comments. The original pattern did not match the elements properly, but this variation should. Interestingly, the pattern works whether or not you add the extraneous \ before the quotes, but Richard is correct that it is not needed.

Edit (again): You're right, this pattern is not working on the actual page. Of the three answers, only ridgrunner's returns 24 matches.


Assuming there will be any number of attributes and the href attribute always comes after the id, and the attributes may or may not have their values within quotes, then this one should do the trick:

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);

Edit: Fixed the expression which consumed the end portion of the start tag. (Was [^>]+)


I do not program in c# but here is a regex that works in ruby (I guess you guys have \\ to indicate character classes?)

/<A.*thread_title_(\d+) href=\"([^\"]*)\">([^<]*)/   

EDIT try this one: thread_title_(\d+) href=\"([^\"]*)\"\>(.*?)<\/A> it matches 2 of them in that pastie thing you did. If you have to match complicated things in HTML, regexes are not good, you should use a XML/HTML parser


This should do it...

<a[^>]+thread_title_(?<id>\d+)[^>]+href="(?<link>[^"]*)">(?<name>[^<]*)</a>

Some of the other suggestions were a little too greedy and were matching more than one link at a time with your sample text.

One other thing to point out is the (?<link> notation, which is a named group. It matches the same way as a regular group. But you can then access these groups in C# by their name, or their index.

You can see this in action here...

http://regexhero.net/tester/?id=7855af6f-7774-4a7c-afa2-81c3e24cf496

By the way, use the .NET button at the top of Regex Hero to generate C#, and then the quotes will be properly escaped for you.

0

精彩评论

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

关注公众号