regex to find numbers with unique digits

前端 未结 5 760
忘了有多久
忘了有多久 2020-12-10 13:19

I want to find 10 digit numbers with no repeat digits, for example:

1123456789 //fail, there are two 1\'s
6758951230 //fail, there are two 5\'s
6789012345 //         


        
相关标签:
5条回答
  • 2020-12-10 13:54

    Here's the shortest and efficient regex with less backtracking due to the presence of a ?.

    Works for any length of input:

    !/(.).*?\1/.test(number)
    

    Examples:

    !/(.).*?\1/.test(1234567890) // true
    !/(.).*?\1/.test(1234567490) // false - note that it also works for repeated chars which are not adjacent.
    

    Demo
    - checks for repeated digits
    - opposite of what you want, because rubular doesn't allow a !

    0 讨论(0)
  • 2020-12-10 14:04

    Works every time (I see this question) -

    Revised to define Grp 10 before the (?! \10 ) assertion. \1-\9 are always considered backrefs (> \10, the parenth's must be before it is referenced).
    So made them all the same as well.

    Note- this can be used to find a floating (substring) 10 uinque digit number. Requires no anchors.
    Fyi - With Perl, the \g{#} (or \k'name') syntax could be used before the group is defined, no matter what number the group number is.

     #  "(?:((?!\\1)1)|((?!\\2)2)|((?!\\3)3)|((?!\\4)4)|((?!\\5)5)|((?!\\6)6)|((?!\\7)7)|((?!\\8)8)|((?!\\9)9)|((?!\\10)0)){10}"
    
    
     (?:
          (                      # (1)
               (?! \1 )
               1
          )
       |  (                      # (2)
               (?! \2 )
               2
          )
       |  (                      # (3)
               (?! \3 )
               3
          )
       |  (                      # (4)
               (?! \4 )
               4
          )
       |  (                      # (5)
               (?! \5 )
               5
          )
       |  (                      # (6)
               (?! \6 )
               6
          )
       |  (                      # (7)
               (?! \7 )
               7
          )
       |  (                      # (8)
               (?! \8 )
               8
          )
       |  (                      # (9)
               (?! \9 )
               9
          )
       |  (                      # (10)
               (?! \10 )
               0
          )
     ){10}
    
    0 讨论(0)
  • 2020-12-10 14:15

    This regex works:

    ^(?!.*(.).*\1)\d{10}$
    

    This uses an anchored negative look ahead with a back reference to assert that there are no repeating characters.

    See a live demo working with your examples.

    In java:

    if (str.matches("^(?!.*(.).*\\1)\\d{10}"))
        // number passes
    
    0 讨论(0)
  • 2020-12-10 14:15

    Try this one (?:([0-9])(?!.*\1)){10}, this will work if you're validating numbers one at a time.

    This should work (?:([0-9])(?!\d*\1)){10} to search for each occurance of an unique 10-digit sequence, but it will fail with 12345678901234567890, will find the last valid part 1234567890 instead of ignoring it.

    Source and explanations: https://stackoverflow.com/a/12870549/1366360

    0 讨论(0)
  • 2020-12-10 14:15

    lancemanfv regex reference https://stackoverflow.com/a/12870549/1366360 is a great one, but the suggested regex is slightly off.

    Instead try

    ^(?:([0-9])(?!.*\1)){10}$ 
    

    This will match any string that begins and ends with 10 digits that are all different.

    If you want to check (and extract) if a longer string contains a 10 digit number with each number different use this

    ((?:([0-9])(?!.*\2)){10})*
    

    You can then use a numbered reference to extract the matching number

    0 讨论(0)
提交回复
热议问题