Sionnach733
Sionnach733

Reputation: 4736

regex to find numbers with unique digits

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 //pass, each digit occurs once only. 

at the moment I am using regex but can only match 10digits numbers(it doesnt check for duplicates. I am using this regex:

[0-9]{10}

Can this be done with regex or is there a better way to achieve this?

Upvotes: 12

Views: 15235

Answers (5)

Om Shankar
Om Shankar

Reputation: 8069

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 !

Upvotes: 1

user557597
user557597

Reputation:

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}

Upvotes: 0

mdolbin
mdolbin

Reputation: 936

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

Upvotes: 1

bdrx
bdrx

Reputation: 943

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

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

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

Upvotes: 11

Related Questions