RegEx to find two or more consecutive chars
I need to determine if a string contains two or more consecutive alpha chars. Two or more [a-zA-Z]
side by side.
Example:
"ab" -> valid
"a1" -> invalid
"a b" -> invalid
"a"-> invalid
"a ab" -> valid
"11" -> invalid
This should do the trick:
[a-zA-Z]{2,}
[a-zA-Z]{2,} does not work for two or more identical consecutive characters. To do that, you should capture any character and then repeat the capture like this:
(.)\1
The parenthesis captures the . which represents any character and \1 is the result of the capture - basically looking for a consecutive repeat of that character. If you wish to be specific on what characters you wish to find are identical consecutive, just replace the "any character" with a character class...
([a-zA-Z])\1
Finds a consecutive repeating lower or upper case letter. Matches on "abbc123" and not "abc1223". To allow for a space between them (i.e. a ab), then include an optional space in the regex between the captured character and the repeat...
([a-z]A-Z])\s?\1
Personnaly (as a nooby) I've used:
[0-9][0-9]+.
But the one from Simon, is way better ! =D