[英]Regex in C# doesn't match when there more than one space or there is comma and space
我使用了C#,我想匹配3个由逗号(也许是\\否)或\\和空格(可能是\\否和一个或多个)分隔的双打
我做了:
Regex regex = new Regex(@"\d+[,|\s*]\d+[,|\s*]\d+");
Match match = regex.Match(mystr.Text);
问题是以下文本不匹配:
33 44 55
(33和44之后有两个空格)
另外,比赛没有成功:
33, 44, 55
(逗号后还有一个空格)
有什么建议吗?
谢谢!
方括号中的正则表达式是错误的,您可以尝试以下操作:
\d+[,\s]*\d+[,\s]*\d+
这样的功能适合您吗?
(\d{2},?\s+){2}\d{2}
它匹配两位数,然后匹配零个或一个逗号,并带有多个空格,并且匹配两次并以两位数结尾吗?
这是一个简单的解决方案
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()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.