1 2 Previous Next 15 Replies Latest reply on Sep 20, 2007 10:13 PM by 807605

    regex capturing group captured more times, need all

      Hi all,
      I have a following-like string "array[10][20][30]" and I need to use a regex to get:

      Any way how to achieve this? The pattern for "array[10]" is much simplier, but the capturing group witch is matched more times is always replaced by following match, so only the last such match can be obtained.

      Code snapshot looks as follows:

                Pattern p = Pattern.compile("(\\w+)(?:\\[(\\d+)\\])+");
                Matcher m = p.matcher("array[10][20][30]");
                if (m.matches()) {
                     for (int i=1; i<=m.groupCount(); i++) {
                          System.out.println("group " + i + "=" + m.group(i));

      group 1=array
      group 2=30

      Any idea how to capture all the groups?
      Any help would be appreciated..

        1 2 Previous Next