Parsing a random string looking for repeating sequences using Java and Regex.
Consider strings:
aaabbaaacccbb
I\'d like to find a regular expression
This does it:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s = "aaabbaaacccbb";
find(s);
String s1 = "RonRonRonJoeJoe .... ,,,,";
find(s1);
System.err.println("---");
String s2 = "RonBobRonJoe";
find(s2);
}
private static void find(String s) {
Matcher m = Pattern.compile("(.+)\\1+").matcher(s);
while (m.find()) {
System.err.println(m.group());
}
}
}
OUTPUT:
aaa
bb
aaa
ccc
bb
RonRonRon
JoeJoe
....
,,,,
---