如果我interprete您的要求是正确的,下面的正则表达式没有匹配的无效输入相匹配的有效输入。
^\D*\d*\D*\d?(?!\d+)$
解释如下
> # ^\D*\d*\D*\d?(?!\d+)$
> #
> # Options: case insensitive;^and $ match at line breaks
> #
> # Assert position at the beginning of a line (at beginning of the string or
> after a line break character) «^»
> # Match a single character that is not a digit 0..9 «\D*»
> # Between zero and unlimited times, as many times as possible, giving back
> as needed (greedy) «*»
> # Match a single digit 0..9 «\d*»
> # Between zero and unlimited times, as many times as possible, giving back
> as needed (greedy) «*»
> # Match a single character that is not a digit 0..9 «\D*»
> # Between zero and unlimited times, as many times as possible, giving back
> as needed (greedy) «*»
> # Match a single digit 0..9 «\d?»
> # Between zero and one times, as many times as possible, giving back as
> needed (greedy) «?»
> # Assert that it is impossible to match the regex below starting at this
> position (negative lookahead)
> «(?!\d+)»
> # Match a single digit 0..9 «\d+»
> # Between one and unlimited times, as many times as possible,
> giving back as needed (greedy) «+»
> # Assert position at the end of a line (at the end of the string or before a
> line break character) «$»
你的第二个例子是错误的,不是吗? – 2010-10-18 13:16:16
@GáborLipták:他只想检查连续的数字,以便正确评估。 – poke 2010-10-18 13:17:53
难道你不能通过搜索'/ \ d {2,} /'真正简化这个并且否定结果吗? – thetaiko 2010-10-18 13:18:34