开发者

Make groups with regular expression like in perl?

开发者 https://www.devze.com 2023-01-12 16:48 出处:网络
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\".

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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消