我想匹配一个字符串,我分成4组:
1.) group has a "-"
2.) group has any char
3.) group has a ":"
4.) group has any char
Run Code Online (Sandbox Code Playgroud)
我试过这个:
Regex regex = new Regex("^[-][.*][:][.*]*$");
bool isMatch = regex.IsMatch("-jobid:3");
Run Code Online (Sandbox Code Playgroud)
isMatch是假的.
我的模式有什么问题?
这里的错误是.*不应该括在括号中.
这个:
[.*]
Run Code Online (Sandbox Code Playgroud)
意思是:
这个:
.*
Run Code Online (Sandbox Code Playgroud)
意思是:
此外,如果一个点中只有一个合法字符,通常不需要将其括在括号中.
所以试试这个表达式:
new Regex("^-.*:.*$");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |