Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regexp: How to extract $1, $2 variables from match

Tags:

c#

.net

c#-3.0

mono

Currently .NET uses their own nonstandard capturing group naming convention which is a dog. I would like to enable the standard use of $1, $2 capture groups in C#.

Is their any way of doing it, or if not, is their any thirdparty regexp engines available for use, which do implement that kind of functionality.

like image 715
scope_creep Avatar asked Aug 18 '10 14:08

scope_creep


1 Answers

It's not 100% clear what you're looking for but it sounds like you want the ability to get the value for a given group in a matched regular expression. This is definitely possible in C# (and .Net in general).

For example.

var regex = new Regex(@"(a+)(\d+)");
var match = regex.Match("a42");
Console.WriteLine(match.Groups[1].Value); // Prints a
Console.WriteLine(match.Groups[2].Value); // Prints 42

While I don't use Mono regularly, I would be very surprised if this didn't work there as well.

like image 72
JaredPar Avatar answered Sep 20 '22 22:09

JaredPar