tags:

views:

63

answers:

2

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.

+1  A: 

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.

JaredPar
I'm looking for a way in to implement the standard PCRE method of accessing named group as $0, $1, $2, $3 in C#
scope_creep
+2  A: 

According to C# Regular Expressions, Using Replacement Strings with Regex.Replace, you can use the following code:

string s = Regex.Replace("  abra  ", @"^\s*(.*?)\s*$", "$1");
kiamlaluno