Kellen Stuart
Kellen Stuart

Reputation: 8893

Regex - Match if there is any repeating lowercase letter

What is a more succinct way of writing this regex? I wrote a script to generate this monstrosity, but I would prefer to understand the cleaner solution.

a{2,}|b{2,}|c{2,}|d{2,}|e{2,}|f{2,}|g{2,}|h{2,}|i{2,}|j{2,}|k{2,}|l{2,}|m{2,}|n{2,}|o{2,}|p{2,}|q{2,}|r{2,}|s{2,}|t{2,}|u{2,}|v{2,}|w{2,}|x{2,}|y{2,}|z{2,}

For example, it would match:

abcbb
ababcaa
abcazzba

But it would not match

abcabcdef
abcde
abc

Upvotes: 0

Views: 213

Answers (1)

person_v1.32
person_v1.32

Reputation: 3167

You might be able to use ([a-z])\1+.

[a-z] matches any lowercase letter, and wrapping it in parentheses creates a capture group, which can then be referenced with \1. + means to make sure the previous token (which is whatever the first capture group captured) appears at least once.

Together, this looks for a lowercase letter then checks if the next letters are the same as the first.

https://regexr.com/65fu2

Upvotes: 3

Related Questions