C # regexp: how to extract $ 1, $ 2 variables from a match

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

This is any way to do this, and if not, that is, any third-party regexp mechanisms available for use that implement such functionality.

+5
source share
2 answers

It does not 100% clear what you are looking for, but it looks like you want to get the value for this group in a matched regular expression. This is possible in C # (and .Net in general).

For instance.

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

Mono , , .

+7

#, Regex.Replace, :

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

All Articles