Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Short and sweet, using a negative lookahead:</p> <pre><code>/^(?!.*([1-9]).*\1)[1-9]{9}$/ </code></pre> <ul> <li><code>[1-9]</code> is the character class for nonzero digits - equivalent to <code>[123456789]</code></li> <li><code>.*</code> matches any string of any length.</li> <li><code>.*([1-9]).*\1.*</code> matches any string with that contains at least two occurrences of the same nonzero digit <ul> <li>a nonzero digit is matched and captured by <code>([1-9])</code></li> <li>a repeat of that nonzero digit is matched by <code>\1</code>, a <strong>back-reference</strong> to the first captured match.</li> <li>the <code>.*</code> matches the arbitrary padding before, and between the nonzero digit and its repeat.</li> </ul></li> <li><code>(?!<i>&lt;pattern&gt;</i>)</code> matches any position where the contained pattern <em>doesn't</em> match. This is a <strong>negative lookahead</strong>, as it only matches a <em>position</em> in the string, and doesn't consume any of it - just looks ahead to compare it with the contained pattern.</li> <li><code>[1-9]{9}</code> matches nine nonzeo digits. <ul> <li><code><i>&lt;pattern&gt;</i>{9}</code> means match the preceding pattern 9 times.</li> </ul></li> <li><code>^<i>&lt;pattern&gt;</i>$</code> matches any string that exactly matches the contained pattern (rather than contains a substring that matches the pattern) <ul> <li><code>^</code> matches the position at the beginning of a string OR the beginning of a line</li> <li><code>$</code> matches the position at the end of a string OR the end of a line</li> </ul></li> </ul> <p>So combined, we check to make sure that it's not repeating any digits, then we check that it's only digits. Since it's 9 digits long, and none repeat, all must show up exactly once. That's the <a href="http://en.wikipedia.org/wiki/Pigeonhole_principle" rel="noreferrer">pigeonhole principle</a> at work!</p> <p>The syntax for your specific regular expression engine may vary. The above is a <a href="http://en.wikipedia.org/wiki/PCRE" rel="noreferrer">PCRE</a> (supported in Perl, Ruby, and a bunch of different other languages). <a href="http://en.wikipedia.org/wiki/Regular_expression#POSIX" rel="noreferrer">Posix regular expressions</a> have slightly different syntax. Not all engines support negative lookaheads, but most support back-references. Neither are part of the definition of formal theoretic regular expressions, but are very convenient.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload