[Solved] RegExp for matching if a text has only one type of letter, example “KKKK”


You can do this using groups and back-references.

My examples are in Java’s regex dialogue, but you can adapt it to any dialect that supports back-references. For example some engines need \(...\) instead of (...).

First you need a group, which you denote by surrounding in parentheses:

(.)

This matches any single character. If you want it to specifically be upper-case letters, you could use (A-Z) instead.

Next you need to match a second character that is the same as the contents of the group. You can back-reference the contents of the first match using \1:

(.)\1

This will match AA or BB but not AB. And not A, since the second character is compulsory.

Next you need to specify that the second character occurs any number of times, including zero. That’s done with *.

(.)\1*

This matches A, B, AA, BB, AAA, BBBBBBB but not AB or AAB.

In Java, the \ has to be escaped:

 Pattern pattern = Pattern.compile("(.)\\1*");

Your regex engine might:
– use this to search the input string for a part that matches (so BAAAAB matches — it finds the AAAA)
– or it might require the whole string to match the pattern (so BAAAAB does not match).

If you want the second behaviour from an engine that does the first, add .* either side of the pattern.

If you want the first behaviour from an engine that does the second, add ^ at the start and $ at the end of the pattern, these match the start and end of the line, respectively.

solved RegExp for matching if a text has only one type of letter, example “KKKK”