问题
I would like it to match:
aaaaaa
bb
c
but not:
aaabaaa
cd
...
回答1:
Assuming the regex engine supports back-references,
^(.)\1*$
In Java it would be
theString.matches("(.)\\1*")
回答2:
Using back references:
(.)(\1)*
Read: match any character followed by that same character 0 or more times.
Depending on the regexp engine and your needs, you might want to anchor the regex to only match the whole string, not substrings.
回答3:
If you want to capture what you match, it is ^((.)\2*)$
回答4:
Just for contributing to this question, you can use the BackRefence:
(\w+)\s+\1
It checks repeated words separated by whitespace.
来源:https://stackoverflow.com/questions/3258444/how-can-i-write-the-regex-all-characters-are-the-same