简体   繁体   English

括号不捕获c#regex组

[英]Parentheses not capturing c# regex groups

Trying to extract the name and id from a string with the following code: 尝试使用以下代码从字符串中提取nameid

Regex.Matches
    "id: 123456, name: some dude",
    @"^id: (?<id>\d+), name: (?<name>[a-z]+(\s[a-z]+)?)"
);

However, the number of groups and captures is 1, and there are no named captures. 但是,组和捕获的数量是1,并且没有命名捕获。 What am I missing? 我错过了什么?

Edit: Found the answer (thanks everyone!) but for future reference, it turns out it's because I completely misunderstood the API and was accessing the matches wrong. 编辑:找到答案(谢谢大家!)但是为了将来的参考,事实证明这是因为我完全误解了API并且访问错误的匹配。 I was doing stuff like this: 我在做这样的事情:

Console.WriteLine(matches.Count); // 1
foreach( Group g in matches ) {
    Console.WriteLine(g.Captures.Count); //1
    foreach( Capture c in g.Captures ) {
        Console.WriteLine(c.Value); // The whole string
    }
}

But now I know better! 但现在我知道的更好!

There is probably something wrong with the way you look for matches as your regex look ok: 当你的正则表达式看起来没问题时,你寻找匹配的方式可能有问题:

Regex.Matches(
    "id: 123456, name: some dude",
    @"^id: (?<id>\d+), name: (?<name>[a-z]+(\s[a-z]+)?)"
)[0].Groups.Cast<Group>().Skip(2).Select(x => new {x.Name, x.Value})

Produces: 生产:

Name  Value
    id    123456 
    name  some dude

If you just need particular one - Regex.Matches(...)[0].Groups[2] or .Groups["id"] . 如果您只需要特定的一个 - Regex.Matches(...)[0].Groups[2].Groups["id"]

Note that your code uses Matches that returns all matches, if you just expect single match - use Match as shown in Michael Randall answer 请注意,您的代码使用Matches返回所有的比赛,如果你只是希望一根火柴-用Match如图迈克尔·兰德尔 回答

This works just fine 这很好用

String sample = "id: 123456, name: some dude";
Regex regex = new Regex(@"^id: (?<id>\d+), name: (?<name>[a-z]+(\s[a-z]+)?)");

Match match = regex.Match(sample);

if (match.Success)
{
   Console.WriteLine(match.Groups["id"].Value);
   Console.WriteLine(match.Groups["name"].Value);
}

And when .Net Fiddle works again you can demo it here 当.Net Fiddle再次运作时, 您可以在这里进行演示


For Completness 对于完整性

Regex.Match Method (String) Regex.Match方法(String)

Searches the specified input string for the first occurrence of the regular expression specified in the Regex constructor. 在指定的输入字符串中搜索Regex构造函数中指定的第一次出现的正则表达式。

Regex.Matches Method Regex.Matches方法

Searches an input string for all occurrences of a regular expression and returns all the matches. 在输入字符串中搜索所有正则表达式,并返回所有匹配项。

Match.Groups Property Match.Groups属性

Gets a collection of groups matched by the regular expression. 获取由正则表达式匹配的组的集合。 Groups can then be retrieved from the GroupCollection object 然后可以从GroupCollection对象中检索组

GroupCollection.Item Property (String) GroupCollection.Item属性(String)

Enables access to a member of the collection by string index. 允许通过字符串索引访问集合的成员。 GroupName can be either the name of a capturing group that is defined by the (?<name>) element in a regular expression, or the string representation of the number of a capturing group that is defined by a grouping construct GroupName可以是由正则表达式中的(?<name>)元素定义的捕获组的名称,也可以是由分组构造定义的捕获组的编号的字符串表示形式

You should use Match instead of Matches , and use non-capturing groups (?:) in your regex. 您应该使用Match而不是Matches ,并在正则表达式中使用非捕获组(?:)

Regex demo 正则表达式演示

String input = "id: 123456, name: some dude";

var output = Regex.Match(input, @"^id: (?<id>\d+), name: (?<name>.+)");

System.Console.WriteLine(output.Groups["id"]);
System.Console.WriteLine(output.Groups["name"]);

Output: 输出:

123456 
some dude

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM