Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Match Collection multiple matches

Tags:

c#

regex

I'm trying to retrieve all text between <td> and</td>, but I only get the first match in my collection. Do I need a * or something? Here is my code.

string input = @"<tr class=""row0""><td>09/08/2013</td><td><a href=""/teams/nfl/new-england-patriots/results"">New England Patriots</a></td><td><a href=""/boxscore/2013090803"">L, 23-21</a></td><td align=""center"">0-1-0</td><td align=""right"">65,519</td></tr>";

string pattern = @"(?<=<td>)[^>]*(?=</td>)";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
    try
    {
        listBoxControl1.Items.Add(matches.ToString());
    }
    catch { }
}
like image 341
Trey Balut Avatar asked Sep 15 '13 15:09

Trey Balut


People also ask

What is MatchCollection C#?

Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string. The collection is immutable (read-only) and has no public constructor. The Matches(String) method returns a MatchCollection object.

What is regex matching?

A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

How do I return a regex match?

You can retrieve subsequent matches by repeatedly calling the returned Match object's Match. NextMatch method. You can also retrieve all matches in a single method call by calling the Regex. Matches(String, Int32) method.


1 Answers

Use the following regex expression:

string input = "<tr class=\"row0\"><td>09/08/2013</td><td><a href=\"/teams/nfl/new-england-patriots/results\">New England Patriots</a></td><td><a href=\"/boxscore/2013090803\">L, 23-21</a></td><td align=\"center\">0-1-0</td><td align=\"right\">65,519</td></tr>";

string pattern = "(<td>)(?<td_inner>.*?)(</td>)";

MatchCollection matches = Regex.Matches(input, pattern);

foreach (Match match in matches) {
    try {
        Console.WriteLine(match.Groups["td_inner"].Value);
    }
    catch { }
}
like image 89
Gary C. Avatar answered Sep 28 '22 10:09

Gary C.