Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 903
jflood.net Avatar asked Apr 26 '12 07:04

jflood.net


People also ask

How do you reference a named group in regex?

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.

What are capture groups regex?

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" .

What is first capturing group in regex?

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.

What is capturing group in regex Javascript?

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.


2 Answers

 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.

like image 102
Yuriy Faktorovich Avatar answered Sep 24 '22 21:09

Yuriy Faktorovich


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.

like image 45
svick Avatar answered Sep 24 '22 21:09

svick