[英]Regular Expression doesn't Match with string
I am trying to use Regular Expressions to find a string sequence inside a string. 我正在尝试使用正则表达式在字符串中查找字符串序列。
The pattern i am looking for is: 我正在寻找的模式是:
dd.dd.dddd dd:dd:dd //d is a digit from 0-9 dd.dd.dddd dd:dd:dd // d是0-9的数字
my regex is: 我的正则表达式是:
Regex r = new Regex(@"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
I am now trying to check, if the string "27.11.2014 09:14:59" is Matching to the regex, but sadly it isn't matching. 我现在正在尝试检查,如果字符串“27.11.2014 09:14:59”与正则表达式匹配,但遗憾的是它不匹配。
string str= "27.11.2014 09:14:59";
Regex r = new Regex(@"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
test = r.IsMatch(str,0);
//output: test=false
Anyone knows why the String is not Matching with that regular expression? 任何人都知道为什么String不匹配该正则表达式?
\\d[0-9]{2}
matches three digits: \\d[0-9]{2}
匹配三个数字:
\d first digit
[0-9] second digit
{2} causes the previous expression ([0-9]) to match again
If you remove all occurences of \\d
, your pattern should work. 如果删除
\\d
所有出现,您的模式应该有效。 You should escape all dots .
你应该逃脱所有的点
.
though, because right now they match any character, not just a .
但是,因为现在他们匹配任何角色,而不仅仅是一个角色
.
. 。
As Rawing already said, the upper Regular expression is trying to match 3 digits instead of one. 正如Rawing已经说过的那样,上面的正则表达式试图匹配3个数字而不是1个数字。 for everyone who want to know how the regular expression should look like:
对于想要了解正则表达式应该如何的每个人:
@"(\d{2}.\d{2}.\d{4}\s\d{2}:\d{2}:\d{2})$"
Thats working, at least for me. 那是工作,至少对我而言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.