正则表达式 PCRE:验证不包含 3 个或更多连续数字的字符串

发布于 2024-09-27 22:30:09 字数 411 浏览 4 评论 0原文

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

rrfefzef        => TRUE
rrfef12ze1      => TRUE
rrfef1zef1      => TRUE
rrf12efzef231   => FALSE
rrf2341efzef231 => FALSE

到目前为止,我已经编写了以下正则表达式:

@^\D*(\d{0,2})?\D*$@

它仅匹配仅出现一次 \d{0,2}

如果其他人有时间帮助我解决此问题,我将不胜感激:)

问候,

I've searched the questions but can't find an answer. I need a pattern that, used with php preg_match function, only match strings that does not contains 3 or more consecutive digits, e.g.:

rrfefzef        => TRUE
rrfef12ze1      => TRUE
rrfef1zef1      => TRUE
rrf12efzef231   => FALSE
rrf2341efzef231 => FALSE

So far, I have wrote the following regex:

@^\D*(\d{0,2})?\D*$@

it only match the strings that have only one occurrence of \d{0,2}

If someone else has time to help me with this, I would appreciate it :)

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

未蓝澄海的烟 2024-10-04 22:30:09

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

或者仅在没有连续数字时才使用负向前查找来匹配: ^(?!.*\d {2}).*$

Reject the string if it has two or more consecutive digits: \d{2,}

Or use negative lookahead to match only if there are no consecutive digits: ^(?!.*\d{2}).*$

淡忘如思 2024-10-04 22:30:09
/^(.(?!\d\d\d))+$/

匹配后面不跟三位数字的所有字符。 示例

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

Matches all characters that are not followed by three digits. Example.

简美 2024-10-04 22:30:09

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

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

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

You could search for \d\d, which will match on all the bad strings. Then you can adjust your further program logic to correctly react to that.

If you really need a "positive" match on strings that contain adjacent digits, this should also work:

^\D?(\d?\D)*$
时光病人 2024-10-04 22:30:09

是否有什么因素阻止您简单地在 preg_match() 函数前添加“!”前缀,从而反转布尔结果?

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

是不是容易多了...

Is there anything stopping you from simply prefixing the preg_match() function with "!", thereby reversing the boolean result?

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

Is so much easier...

梦里梦着梦中梦 2024-10-04 22:30:09

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

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

If I interprete your requirement correct, following regex matches your valid inputs without matching the invalid inputs.

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

is explained as follows

> # ^\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) «$»
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文