我正在努力使用正则表达式模式,将文本从字符串中拉出到命名组中.
一个(有点武断的)例子将最好地解释我正在努力实现的目标.
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;
Run Code Online (Sandbox Code Playgroud)
我似乎无法使正则表达式模式起作用,但我认为上面解释得很好.基本上我需要得到人名,游戏数量等.
谁能解决这个问题并解释他们制定的实际正则表达式模式?
Yur*_*ich 28
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>.*?)$";
Run Code Online (Sandbox Code Playgroud)
其他帖子提到了如何拉出组,但这个正则表达式匹配您的输入.