Java regex - overlapping matches

Make the matcher attempt to start its next scan from the latter \d+.

Matcher m = Pattern.compile("\\d+\\D+(\\d+)").matcher("2abc3abc4abc5");
if (m.find()) {
    do {
        allMatches.add(m.group());
    } while (m.find(m.start(1)));
}

Not sure if this is possible in Java, but in PCRE you could do the following:
(?=(\d+\D+\d+)).

Explanation
The technique is to use a matching group in a lookahead, and then "eat" one character to move forward.

  • (?= : start of positive lookahead
    • ( : start matching group 1
      • \d+ : match a digit one or more times
      • \D+ : match a non-digit character one or more times
      • \d+ : match a digit one or more times
    • ) : end of group 1
  • ) : end of lookahead
  • . : match anything, this is to "move forward".

Online demo


Thanks to Casimir et Hippolyte it really seems to work in Java. You just need to add backslashes and display the first capturing group: (?=(\\d+\\D+\\d+)).. Tested on www.regexplanet.com:

enter image description here


The above solution of HamZa works perfectly in Java. If you want to find a specific pattern in a text all you have to do is:

String regex = "\d+\D+\d+";

String updatedRegex = "(?=(" + regex + ")).";

Where the regex is the pattern you are looking for and to be overlapping you need to surround it with (?=(" at the start and ")). at the end.