154

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
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
LRB
  • 1,559
  • 2
  • 9
  • 4

4 Answers4

313

This should do the trick:

[a-zA-Z]{2,}
Simon
  • 31,675
  • 9
  • 80
  • 92
15

[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
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
James Anderson
  • 161
  • 1
  • 3
  • 1
    I'm quite sure you missunderstood the question as it is about two letters. Your regex is for a different case: two similar letters! – csabinho Nov 20 '19 at 19:15
  • 2
    This answers the actual question and was what I was looking for, many thanks! – MS Berends Jun 01 '22 at 20:12
7

Personally I've used:

[0-9][0-9]+.

But the answer from Simon, is way better.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Mc Kazou
  • 71
  • 1
  • 1
-6

I'm pretty sure you can just use [A-z] instead of the [a-zA-Z] to get small and upper case alpha chars http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Kevin
  • 457
  • 4
  • 12
  • 31
  • 33
    -1: This is another wonderful example of why you should never use w3schools for reference or education. `[A-z]` matches more than letters. Specifically, it also matches square brackets, backslashes, carets, underscores and backticks. – Tim Pietzcker Aug 31 '12 at 22:01
  • 1
    Indeed, what @TimPietzcker says is correct; the characters he mentioned come between `Z` and `a`, so would be (erroneously) included in such a pattern. I second the recommendation: *Do not use w3schools!* It has a lot of subtly bad information like this. – Andrew Barber Aug 31 '12 at 22:03
  • 8
    Ouch first zing on Stack, it stings more than I thought. So by includeing A-z it matches everything from [A-Z a bunch of stuff I don't want and a-z] all the charcodes between capitals and non caps? http://www.danshort.com/ASCIImap/ – Kevin Aug 31 '12 at 22:28
  • so I can say something like [!-+]? Also is there a way to access the charcode inside a regex something like [65-90] which would be the same as [A-Z]? – Kevin Aug 31 '12 at 22:50
  • @Kevin `[65-90]` will match `5`-`9`(which also includes `6`) and `0`! – csabinho Nov 20 '19 at 23:17
  • Please see https://stackoverflow.com/questions/4923380/difference-between-regex-a-z-and-a-za-z – Atzi Oct 01 '21 at 18:49