简体   繁体   中英

Make groups with regular expression like in perl?

In Perl if I use this regex /(\\w+)\\.(\\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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM