Search code examples
javajavascriptregexcapturing-group

Regex fails to capture all groups


Using java.util.regex (jdk 1.6), the regular expression 201210(\d{5,5})Test applied to the subject string 20121000002Test only captures group(0) and does not capture group(1) (the pattern 00002) as it should, given the code below:

Pattern p1 = Pattern.compile("201210(\\d{5,5})Test");
Matcher m1 = p1.matcher("20121000002Test");

if(m1.find()){

    for(int i = 1; i<m1.groupCount(); i++){         
    System.out.println("number = "+m1.group(i));            
    }
}

Curiously, another similar regular expression like 201210(\d{5,5})Test(\d{1,10}) applied to the subject string 20121000002Test0000000099 captures group 0 and 1 but not group 2.

On the contrary, by using JavaScript's RegExp object, the exact same regular expressions applied to the exact same subject strings captures all groups, as one could expect. I checked and re-checked this fact on my own by using these online testers:

Am I doing something wrong here? Or is it that Java's regex library really sucks?


Solution

  • m1.groupCount() returns the number of capturing groups, ie. 1 in your first case so you won't enter in this loop for(int i = 1; i<m1.groupCount(); i++)

    It should be for(int i = 1; i<=m1.groupCount(); i++)