.NET Regular Expressions - Shorter match

Posted by Xavier on Stack Overflow See other posts from Stack Overflow or by Xavier
Published on 2010-05-28T03:25:32Z Indexed on 2010/05/28 3:31 UTC
Read the original article Hit count: 154

Filed under:
|
|

Hi Guys,

I have a question regarding .NET regular expressions and how it defines matches. I am writing:

var regex = new Regex("<tr><td>1</td><td>(.+)</td><td>(.+)</td>");
if (regex.IsMatch(str))
{
    var groups = regex.Match(str).Groups;
    var matches = new List<string>();
    for (int i = 1; i < groups.Count; i++)
        matches.Add(groups[i].Value);

    return matches;
}

What I want is get the content of the two following tags. Instead it returns:

[0]: Cell 1</td><td>Cell 2</td>...
[1]: Last row of the table

Why is the first match taking </td> and the rest of the string instead of stopping at </td>?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET