正则表达式检查重复的数字 [英] Regex to check for a repeating digit

查看:159
本文介绍了正则表达式检查重复的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,用户必须在其中指定PIN才能登录.创建PIN时,只有3个要求:

I have a Java application where users must specify a PIN to log in. When creating the PIN, there are only 3 requirements:

  • 必须为6位数字:

  • Must be 6 digits:

\\d{6}

  • 不得包含4个或更多序列号:

  • Must not have 4 or more sequential numbers:

    \\d*(0123|1234|2345|3456|4567|5678|6789)\\d*
    

  • 数字不能重复3次或更多次(例如000957或623334或514888):这就是我被困住的地方...
  • 我尝试过:

    \\d*(\\d)\\1{3}\\d*
    

    但是我相信 \ 1 正在查看与 \ d * 的初始匹配,而不是(\ d)的第二个匹配

    but I believe the \1 is looking at the initial match to the \d* not the second match of (\d).


    使用的答案:我已更新为使用:


    Answer used: I have updated to using:

    \\d{6}
    (0123|1234|2345|3456|4567|5678|6789|9876|8765|7654|6543|5432|4321|3210)
    \\d*?(\\d)\\1{2,}\\d*
    

    要满足最初提出的要求,再加上一些我没想到的要求!感谢您的所有帮助

    To satisfy the initially stated requirements plus a few I hadn't thought of! Thanks for all the help

    推荐答案

    由于第一个\ d与第一个数字匹配,因此您的正则表达式略有偏离.之后,您只想再匹配2个.

    Your regex is slightly off, since the first \d will match the first number. You only want to match 2 more after that.

    \\d*(\\d)\\1{2}\\d*
    

    应该可以解决问题.

    快速如果要按顺序匹配2个或多个数字,只需将逗号添加到计数中,而无需指定最大数字:

    Quick edit: If you want to match 2 or more numbers in sequence, just add a comma to your count, without specifying a maximum number:

    \\d*(\\d)\\1{2,}\\d*
    

    或者至少,这在Perl中有效.让我们知道你的情况.

    Or at least, this works in Perl. Let us know how you go.

    这篇关于正则表达式检查重复的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆