C# .NET 中命名捕获组的正则表达式模式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10328644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:24:54  来源:igfitidea点击:

What is the regex pattern for named capturing groups in .NET?

c#.netregex

提问by jflood.net

I'm struggling with a regex pattern that will pull out text from a string into named groups.

我正在努力使用正则表达式模式,它将文本从字符串中提取到命名组中。

A (somewhat arbitrary) example will best explain what I'm trying to achieve.

一个(有点随意的)例子最能解释我想要实现的目标。

string input =
    "Mary Anne has been to 949 bingo games. The last was on Tue 24/04/2012. She won with the Numbers: 4, 6, 11, 16, 19, 27, 45";

string pattern =
    @"(?<Person>\w+?) has been to (?<NumberOfGames>\d+?) bingo games. The last was on (?<Day>...?) (?<Date>...?). She won with the Numbers: (?<Numbers>...?)";

Regex regex = new Regex(pattern);
var match = regex.Match(input);

string person = match.Groups["Person"].Value;
string noOfGames = match.Groups["NumberOfGames"].Value;
string day = match.Groups["Day"].Value;
string date = match.Groups["Date"].Value;
string numbers = match.Groups["Numbers"].Value;

I can't seem to get the regex pattern to work, but i think the above explains it well enough. Essentially i need to get the person name, the number of games etc.

我似乎无法让正则表达式模式起作用,但我认为上面已经很好地解释了它。基本上我需要得到人名,游戏数量等。

Can anyone solve this and explain the actual regex pattern they worked out?

任何人都可以解决这个问题并解释他们制定的实际正则表达式模式吗?

采纳答案by Yuriy Faktorovich

 string pattern = @"(?<Person>[\w ]+) has been to (?<NumberOfGames>\d+) bingo games\. The last was on (?<Day>\w+) (?<Date>\d\d/\d\d/\d{4})\. She won with the Numbers: (?<Numbers>.*?)$";

Other posts have mentioned how to pull out the groups, but this regex matches on your input.

其他帖子提到了如何提取组,但此正则表达式与您的输入匹配。

回答by shriek

Assuming the regex works the code for getting the named group would be this:

假设正则表达式工作,获取命名组的代码将是这样的:

string title = match.Groups["Person"].Value;
string drawNumber = match.Groups["NumberOfGames"].Value;

回答by svick

Have a look at the documentation for Result():

看看文档Result()

Returns the expansion of the specified replacement pattern.

返回指定替换模式的扩展。

You don't want any replacement patterns, so this method is not the right solution.

您不想要任何替换模式,因此此方法不是正确的解决方案。

You want to access groups of the match, so do that: there is a Groupsproperty.

您想访问匹配组,所以这样做:有一个Groups属性

With that your code would look like this:

有了它,您的代码将如下所示:

string title = match.Groups["Person"].Value;
string drawNumber = match.Groups["NumberOfGames"].Value;

Also, as russau correctly pointed out, your pattern doesn't match your text: Dateis not just three characters.

此外,正如 russau 正确指出的那样,您的模式与您的文本不匹配:Date不仅仅是三个字符。

回答by russau

Try this:

尝试这个:

string pattern = @"(?<Person>\w+?) has been to (?<NumberOfGames>\d+?) bingo games. The last was on (?<Day>...?) (?<Date>\d+/\d+/\d+). She won with the Numbers: (?<Numbers>...?)";

Your regex isn't matching the date part of the string.

您的正则表达式与字符串的日期部分不匹配。