Regular expressions cannot handle repeating groups, they can return any of the captured groups (in your case, it returned "Ghi" ).
The following example will print:
Abc Def Ghi 12345
Code:
public static void main(String[] args) { String example = "AbcDefGhi12345"; if (example.matches("(([AZ][az]+)+)([0-9]+)")) { Scanner s = new Scanner(example); String m; while ((m = s.findWithinHorizon("[AZ][az]+", 0)) != null) System.out.println(m); System.out.println(s.findWithinHorizon("[0-9]+", 0)); } }
source share