This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Sep 20, 2007 12:55 AM by 807605 RSS

regex capturing group captured more times, need all

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

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()) {
               System.out.println("matched:");
               for (int i=1; i<=m.groupCount(); i++) {
                    System.out.println("group " + i + "=" + m.group(i));
               }

output:
matched:
group 1=array
group 2=30

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

Regards,
Lubos
1 2 Previous Next