Php Regex PCRE:验证不包含3个或更多连续数字的字符串

Php Regex PCRE:验证不包含3个或更多连续数字的字符串,php,regex,pcre,Php,Regex,Pcre,我搜索了这些问题,但找不到答案。我需要一个模式,与php preg_match函数一起使用,只匹配不包含3个或更多连续数字的字符串,例如: rrfefzef => TRUE rrfef12ze1 => TRUE rrfef1zef1 => TRUE rrf12efzef231 => FALSE rrf2341efzef231 => FALSE 到目前为止,我已经编写了以下正则表达式: @^\D*(\d{0,2})?\D*$@

我搜索了这些问题,但找不到答案。我需要一个模式,与php preg_match函数一起使用,只匹配不包含3个或更多连续数字的字符串,例如:

rrfefzef        => TRUE
rrfef12ze1      => TRUE
rrfef1zef1      => TRUE
rrf12efzef231   => FALSE
rrf2341efzef231 => FALSE
到目前为止,我已经编写了以下正则表达式:

@^\D*(\d{0,2})?\D*$@
它只匹配只出现一次
\d{0,2}

如果其他人有时间帮助我,我将不胜感激:)

问候,

/^(.(?!\d\d\d))+$/

匹配所有不后跟三位数字的字符

您可以搜索
\d\d
,它将匹配所有坏字符串。然后,您可以调整进一步的程序逻辑,以正确地对此作出反应

如果您确实需要对包含相邻数字的字符串进行“正”匹配,这也应该可以:

^\D?(\d?\D)*$

是否有任何东西阻止您在
preg_match()
函数前面加上“”前缀,从而反转布尔结果

!preg_match( '/\d{2,}/' , $subject );

更容易…

如果我正确解释您的需求,下面的正则表达式将匹配您的有效输入,而不匹配无效输入

^\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) «$»

如果字符串有两个或多个连续数字,则拒绝该字符串:
\d{2,}


或者只有在没有连续数字的情况下才使用负前瞻匹配:
^(?。*\d{2})。*$

您的第二个示例是错误的,不是吗?@Gábor Lipták:他只想检查连续数字,以便正确计算。您不能通过搜索
/\d{2,}来简化这个过程吗/
并否定结果?@poke-
12
对我来说似乎是连续的数字…@poke我可以在
rrfef12ze1
中看到两个连续的数字。这取决于我们是在考虑他的测试用例,还是他的书面规范-它满足“…不包含两个或更多连续的数字”,但是测试用例似乎是3个或更多。然后,再看一次测试用例,他可能指的是3个或更多的序列数字(即“123”=真,但“134”=假)。事实上,我使用的框架,在这种情况下,封装了preg_匹配返回,所以我不能仅仅反转preg_匹配结果。我本可以使用回调,但我更喜欢正则表达式方法;)