我想匹配一个分为4组的字符串:
1.) group has a "-"
2.) group has any char
3.) group has a ":"
4.) group has any char
我已经试过了:
Regex regex = new Regex("^[-][.*][:][.*]*$");
bool isMatch = regex.IsMatch("-jobid:3");
isMatch为假。
我的模式有什么问题?
这里的错误是.*
不应将其括在方括号中。
这:
[.*]
的意思是:
这:
.*
的意思是:
另外,如果一个斑点中只有1个合法字符,则通常不需要将其括在方括号中。
因此,请尝试使用以下表达式:
new Regex("^-.*:.*$");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句