function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
parkerAPTparkerAPT 

Pattern groups and matching double quotes

Pattern p = Pattern.compile('.*"(.*)"');
Matcher m = p.matcher('Outer quote "test case 1"');
if(m.matches()){
for(Integer i =0 ; i < m.groupCount(); i++){
System.debug('Found Group [' + i + ']: ' + m.group(i));
}
}
Pattern p2 = Pattern.compile('.*("(.*)")');
Matcher m2 = p2.matcher('Outer quote "test case 2"');
if(m2.matches()){
for(Integer i =0 ; i < m2.groupCount(); i++){
System.debug('Found Group [' + i + ']: ' + m2.group(i));
}
}

Can someone explain why these print the following:

First pattern:
Found Group [0]: Outer quote "test case 1" {this is expected, but where is the nested group inside double-quotes?}

Second pattern:
Found Group [0]: Outer quote "test case 2" {expected}
Found Group [1]: "test case 2" {expected, but again, where is the inner group?}

Thanks!