Make groups with regular expression like in perl?
In Perl if I use this regex /(\w+)\.(\开发者_JAVA百科w+)/
on the string "1A3.25D"
, the global vars $1
strores "1A3"
and $2
stores "25D"
.
Is there a way to do this in C#?
Certainly, look at this example:
var pattern = @"^\D*(\d+)$";
var result = Regex.Match("Some text 10", pattern);
var num = int.Parse(result.Groups[1].Value); // 10
Group[0]
is the entire match (in this case the entire line because I use ^
and $
.
If you use Regex.Replace(...)
you can use $X
to incorporate the groups as you are used to :-)
精彩评论