How can I write the regex "All characters are the same"?

I would like it to match:

aaaaaa bb c 

but not:

 aaabaaa cd 

...

+7
java regex
source share
4 answers

Assuming the regex engine supports backlinks,

 ^(.)\1*$ 

In Java, it will be

 theString.matches("(.)\\1*") 
+18
source share

Using backlinks:

 (.)(\1)* 

Count: match any character followed by the same character 0 or more times.

Depending on the regexp mechanism and your needs, you can bind the regex only to the whole string, not substrings.

+8
source share

If you want to capture what you match, this is ^((.)\2*)$

0
source share

Just to contribute to this question, you can use BackRefence:

(\ W +) \ S + \ 1

It checks for repeated words separated by spaces.

0
source share

All Articles