You can use a lookahead and a backreference to solve this. But note that right now you are requiring at least 2 characters. The starting letter and another one (due to the +
). You probably want to make that +
and *
so that the second character class can be repeated 0 or more times:
^(?!.*(.)1)[a-zA-Z][a-zA-Zd._-]*$
How does the lookahead work? Firstly, it's a negative lookahead. If the pattern inside finds a match, the lookahead causes the entire pattern to fail and vice-versa. So we can have a pattern inside that matches if we do have two consecutive characters. First, we look for an arbitrary position in the string (.*
), then we match single (arbitrary) character (.
) and capture it with the parentheses. Hence, that one character goes into capturing group 1
. And then we require this capturing group to be followed by itself (referencing it with 1
). So the inner pattern will try at every single position in the string (due to backtracking) whether there is a character that is followed by itself. If these two consecutive characters are found, the pattern will fail. If they cannot be found, the engine jumps back to where the lookahead started (the beginning of the string) and continue with matching the actual pattern.
Alternatively you can split this up into two separate checks. One for valid characters and the starting letter:
^[a-zA-Z][a-zA-Zd._-]*$
And one for the consecutive characters (where you can invert the match result):
(.)1
This would greatly increase the readability of your code (because it's less obscure than that lookahead) and it would also allow you to detect the actual problem in pattern and return an appropriate and helpful error message.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…