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?
You can reference the contents of the group with the named backreference (? P=name). The question mark, P, angle brackets, and equals signs are all part of the syntax. Though the syntax for the named backreference uses parentheses, it's just a backreference that doesn't do any capturing or grouping.
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .
First group matches abc. Escaped parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex.
Groups group multiple patterns as a whole, and capturing groups provide extra submatch information when using a regular expression pattern to match against a string. Backreferences refer to a previously captured group in the same regular expression.
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.
Have a look at the documentation for 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 Groups
property.
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: Date
is not just three characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With