Java – How to use Java Regex to find all repeating character sequences in a string

javapattern matchingregex

Parsing a random string looking for repeating sequences using Java and Regex.

Consider strings:

aaabbaaacccbb

I'd like to find a regular expression that will find all the matches in the above string:

aaabbaaacccbb
^^^  ^^^

aaabbaaacccbb
   ^^      ^^

What is the regex expression that will check a string for any repeating sequences of characters and return the groups of those repeating characters such that group 1 = aaa and group 2 = bb. Also note that I've used an example string but any repeating characters are valid:
RonRonJoeJoe
… … ,, ,,…,,

Best Answer

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
....
,,,,
---