I realize this post is a little dated, but it came up at the top of a google search on this topic, and since none of the answers provided what I needed, I ended up making a method that did and I simply wanted to add it to this post for future searchers.
This method produces the pattern or patterns that were found and the number of times each pattern repeats in the original string.
When I tried @flakes regex using string.matches(), it only matched true if the patterns were side by side. So it would match 101101 but not 101234101 (it didn't seem to know that the pattern 101 was in there twice.
So, if you simply need to know if your string has the same pattern in it side by side, use this code:
if (myString.matches("^(.+?)(?:\\1)+$")) {
//doSomethingHere
}
Taking the idea of building a substring of patterns to the nth degree, I came up with this method, which basically builds a list of all possible patterns. Then it iterates through that list and checks the original string to see if that pattern is in it. Obviously it will ignore the first hit in the comparison since the pattern will always hit true one time in the source string ... on account of the pattern being created from the source string.
Here is the code, obviously you can massage it for your needs:
private void checkForPattern(String userString) {
String buildString;
LinkedList<String> patterns = new LinkedList<>();
int size = userString.length();
int hits;
int newSize;
String[] coreString = new String[size];
Map<String, Integer> hitCountMap = new HashMap<>();
for (int x = 0; x < size; x++) {
coreString[x] = userString.substring(x, x + 1);
}
for (int index = 0; index < size - 1; index++) {
buildString = coreString[index];
for (int x = index + 1; x < size; x++) {
buildString = buildString + coreString[x];
patterns.add(buildString);
}
}
for (String pattern : patterns) {
String check = userString.replaceFirst(pattern, "");
if (check.contains(pattern)) {
newSize = userString.replaceAll(pattern, "").length();
hits = (size - newSize) / pattern.length();
hitCountMap.put(pattern, hits);
}
}
for (String pattern : hitCountMap.keySet()) {
System.out.println("Pattern: " + pattern +
" repeated " + hitCountMap.get(pattern) +
" times.");
}
}