开发者

Problem with backreferences in C#'s regex

The goal is to extract time and date strings from this:

<strong>Date</strong> - Thursday, June 2 2011 9:00PM<br>

H开发者_开发百科ere's the code:

Match m = Regex.Match(line, "<strong>Date</strong> - (.*) (.*)<br>");
date = m.Captures[0].Value;
time = m.Captures[1].Value;

Thanks to the regex being greedy, it should match the first group all the way up to the last space. But it doesn't. Captures[0] is the whole line and Captures[1] is out of range. Why?


Use Groups, not Captures. Your results will be in Groups[1] and Groups[2].

And personally, I'd recommend naming the groups:

Match m = Regex.Match(line, "<strong>Date</strong> - (?<date>.*) (?<time>.*)<br>");
if( m.Success )
{
    date = m.Groups["date"].Value;
    time = m.Groups["time"].Value;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜