tags:

views:

100

answers:

2

Hello, I need help coming up with a regular expression to match if a string has more than one occurrence of character. I already validated the length of the two strings and they will always be equal. Heres what i mean, for example. The string "aab" and "abb". These two should match the regular expression because they have repeating characters, the "aa" in the first string and the "bb" in the second.

+7  A: 

Since you say "aba"-style repetition doesn't count, back-references should make this simple:

(.)\1+

Would find sequences of characters. Try it out:

java.util.regex.Pattern.compile("(.)\\1+").matcher("b").find(); // false
java.util.regex.Pattern.compile("(.)\\1+").matcher("bbb").find(); // true
vanza
i copied and pasted that and it seems it has a syntax error
Zerobu
vanza
I copied the java code you posted
Zerobu
the `-> false` and `-> true` parts are not part of the code, are you copying them?
Philip Potter
yea i did lol. I changed it now
Zerobu
i didn't need the values to be hard coded though, because the strings could be any value i just used those two as an example.
Zerobu
+1  A: 

If you're checking anagrams maybe a different algorithm would be better.

If you sort your strings (both the original and the candidate), checking for anagrams can be done with a string comparison.

Slinky