What is the difference between a group and a match in .NET RegEx?

What is the difference between Group and Match in .NET RegEx?

+5
source share
2 answers

A Match is an object that indicates a particular regular expression matching (part of) the target text. A Group indicates part of the match if the original regular expression contains group markers (mostly a pattern in parentheses). For example, with the following code:

string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";
Match m = r.Match(text);

m , - 1, (\w+), "" 2 ( (car)), , "".

+6

A Match - , , .

, , . - URL-, , (http), (www.web.com), path (/lol/cats.html) , .

// Example I made up on the spot, probably doesn't work very well
"(?<protocol>\w+)://(?<domain>[^/]+)(?<path>/[^?])"

, , , Regex.Matches( ), , , .

, . :

Match match = pattern.Match(urls);
if (!match.Success) 
    continue;
string protocol = match.Groups["protocol"].Value;
string domain = match.Groups[1].Value;

, ​​ , .

, , MSDN.

+2

All Articles