你的正则表达式,因为它写的,可能不会做你希望它是什么。你需要首先避开反斜杠。例如,在Perl中你会使用它像:
if ($number =~ /[1-9]\d{2}-[1-9]\d{2}-\d{4}/) {
print "matches!\n";
}
你的正则表达式,然后将分解如下:
/[1-9] # Match exactly one of the numbers 1 through 9
\d{2} # Match exactly two digits
- # Match exactly one dash
[1-9] # Match exactly one of the numbers 1 through 9
\d{2} # Match exactly two digits
- # Match exactly one dash
\d{4} # Match exactly four digits
/x
编辑:要告诉你如何你的正则表达式,因为它目前为作品,这是它的故障:
/[1-9] # Match exactly one of the numbers 1 through 9
\\ # Match exactly one \
d{2} # Match exactly two 'd's
- # Match exactly one dash
[1-9] # Match exactly one of the numbers 1 through 9
\\ # Match exactly one \
d{2} # Match exactly two 'd's
- # Match exactly one dash
\\ # Match exactly one \
d{4} # Match exactly four 'd's
/x
看看双反斜杠有多大的区别?
我认为你有额外的backslllashes。 – tchrist 2011-02-15 18:42:43
对不起,这是在Java中完成...忘了提及 – CppLearner 2011-02-15 18:45:43
@JohnWong:正如每一个答案所述,你需要避开反斜杠。将它们从双反斜杠改为单反斜杠。 – CanSpice 2011-02-15 18:48:40