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
2010-08-28 21:19:01
i copied and pasted that and it seems it has a syntax error
Zerobu
2010-08-28 21:28:44
vanza
2010-08-28 21:31:41
I copied the java code you posted
Zerobu
2010-08-28 21:35:04
the `-> false` and `-> true` parts are not part of the code, are you copying them?
Philip Potter
2010-08-28 21:35:43
yea i did lol. I changed it now
Zerobu
2010-08-28 21:38:33
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
2010-08-28 21:44:00
+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
2010-08-28 21:29:21