Define a regex, which matches one digit twice and all others once

Posted by Amin on Stack Overflow See other posts from Stack Overflow or by Amin
Published on 2014-08-23T13:00:33Z Indexed on 2014/08/23 16:21 UTC
Read the original article Hit count: 358

Filed under:
|

As part of a larger regex I would like to match the following restrictions:

  1. The string has 11 digits
  2. All digits are numbers
  3. Within the first 10 digits one number [0-9] (and one only!) must be listed twice

This means the following should match:

12345678914
12235879600

Whereas these should not:

12345678903 -> none of the numbers at digits 1 to 10 appears twice
14427823482 -> one number appears more than twice
72349121762 -> two numbers appear twice

I have tried to use a lookahead, but all I'm managing is that the regex counts a certain digit, i.e.:

(?!.*0\1{2})

That does not do what I need. Is my query even possible with regex?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about regex