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?
Other posts have mentioned how to pull out the groups, but this regex matches on your input.