简体   繁体   中英

c# Regex several numbers with comma

I would like to know how to get more than one comma with regex
I started with the string:

var str = "A:12,18,12 B:10";

now started with regex:

var reg = Regex.Matches(str,@"A:(\d+)\sB:(\d+)").cast<Match>().select(rg => new {
 A = rg.Groups[1].Value,
 B = rg.Groups[2].Value
}).ToList();
 foreach(var lop in reg)
 { 
   Console.Write(lop.A + "-" + lop.B);
 }

I am not good in regex but in B: everything works fine, in A: but not. I think it's the comma Thank you!

Change the regex to

@"A:(\d+(,\d+)*)\sB:(\d+)

ie, add zero or more repetitions ( * ) of ,\\d+ .

If commas can appear in B as well:

@"A:(\d+(,\d+)*)\sB:(\d+(,\d+)*)

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