Hi,
Use a construct called a "negative lookahead".
The above regex
pattern consist of four parts:
^ +
(?!pattern) +
\d{11} +
$.
^ ,
\d{11} and
$ are very basic constructs I expect everybody should know, so we skip them here.
(?!pattern) pattern is called a
negative lookahead. This construct checks if a
pattern (inside brackets) matches the input string,
and if yes - the regex engine reports failure of the whole pattern at this point...
- if no, the regex engine continues trying to match the rest of the pattern (after the negative lookahead).
Lookaheads has one important attribute - they
do not consume characters from the input string,
so when this pattern matches, then the regex engine continues matching next part of the pattern starting from the same input string position.
Inside brackets there is a
(\d)\1{10} pattern - here round brackets constructs a
backreference.
Backreferences allow to reuse part of regex match in other plases of the pattern.
In the above pattern
\1 is a reference to a pattern backreference number 1 (placed in the first () pair in the pattern)
and it is substituted with input characters matched with this pattern inside brackets.
The whole pattern
(\d)\1{10} matches if the first character is a digit (
\d) and the next character -
\1 - repeated 10 times -
{10}
is the same character as the first character matched by pattern \d inside brackets.
Summarizing -
(\d)\1{10} checks if the input stream contains 11 same digits,
if yes - the surrounding pattern
(?!pattern) makes the whole pattern to fail,
otherwise the regex engine continues matching next
\d{11} pattern.
I am not sure if my explanation is clear enough, refer to this page for more details:
http://www.regular-expressions.info/tutorial.html
This is the most excellent regex tutorial I have seen on the web.