简体   繁体   中英

Regex in C# doesn't match when there more than one space or there is comma and space

I used C# and I would like to match 3 doubles seperated by comma(maybe yes\\not) or\\and by spaces(maybe yes\\not and one or more)

I did:

 Regex regex = new Regex(@"\d+[,|\s*]\d+[,|\s*]\d+");
 Match match = regex.Match(mystr.Text);

Issue is that the below text isn't match:

33 44 55 (after 33 and after 44 there is two spaces)

Also, the match didn't catch:

33, 44, 55 (after comma there is also one space)

Any advice?

Thanks!

方括号中的正则表达式是错误的,您可以尝试以下操作:

\d+[,\s]*\d+[,\s]*\d+

Would something like this function for you?

 (\d{2},?\s+){2}\d{2}

It matches 2 digits then zero or one comma, with multiple spaces, and it does it twice and ending with 2 digits?

Here's a simple solution for you

string nmbrs = "33  44";
string numberPattern = @"\d+(?=[,\s]*\d+[,\s]*)\d+";
var matches = Regex.Matches(nmbrs, numberPattern);
List<int> numbersList = new List<int>();
foreach (var match in matches)
{
    numbersList.Add(int.Parse(match.ToString()));
}

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