tags:

views:

187

answers:

4

I would like it to match:

aaaaaa
bb
c

but not:

aaabaaa
cd

...

+5  A: 

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.

sepp2k
Trying Java, but System.out.println("1111111".matches("(.)(\1)*")); prints false
Julio Faerman
@Julio: You need to double escape in java, i.e. use `\\1` instead of `\1`.
sepp2k
Great, thanks a lot
Julio Faerman
+13  A: 

Assuming the regex engine supports back-references,

^(.)\1*$

In Java it would be

theString.matches("(.)\\1*")
KennyTM
Trying java, but System.out.println("1111111".matches("^(.)\1*$")); prints false.
Julio Faerman
@Julio: Try `"^(.)\\1*$"`, with 2 backslashes.
KennyTM
that did it, thanks a lot!
Julio Faerman
@Kenny: In java `^` and `$` mean beginning and end of line, not string. Also regex matches are anchored by default. So just "(.)\\1*" will work.
sepp2k
@sepp2k: by default, `^` and `$` match the beginning and end of the String, not line. When the `(?m)` flag is enabled, they match the beginning and end of a line. But when using `String.matches(...)`, `^` and `$` are not needed: by default, `matches(...)` will match the entire input string.
Bart Kiers
@Bart: The [javadocs for Pattern](http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/regex/Pattern.html) say "^ The beginning of a line" and "$ The end of a line"
sepp2k
@sepp2k, well, then the JavaDoc is wrong. Try it yourself: `System.out.println("a\nb".matches("^a$\nb$"));` prints `false`, while `System.out.println("a\nb".matches("(?m)^a$\nb$"));` prints `true`.
Bart Kiers
@sepp2k, from the JavaDoc as well: *"By default, the regular expressions ^ and $ ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence."*
Bart Kiers
@Bart: You are right. And yeah, I just saw the other bit in the docs.
sepp2k
@sepp2k, it never occurred to me that `^` and `$` are described as you just mentioned: rather confusing from Sun, err, Oracle! :)
Bart Kiers
A: 

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

drewk
A: 

Just for contributing to this question, you can use the BackRefence:

(\w+)\s+\1

It checks repeated words separated by whitespace.

Denis Macedo