Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expressions, groups.

Status
Not open for further replies.

kanghao

IS-IT--Management
Jul 4, 2004
68
KR
Questions.
1. When you say group(n), quantifiers for the group should not be considered. For example (a(b*))+, group(1) is not (a(b*))+ but (a(b*)).
2. If n>0, group(n) returns the last match in the context of the previous match(group(n-1)), if no match in group(n-1), search n-2, on and on.
3. the below.

CharSequence inputStr = "abbabcd abbbabbbbcd";
String patternStr = "(a(b*))+(c*)";

while(matcher.find()) {
System.out.println("input string : " + inputStr);
System.out.println("pattern string : " + pattern.pattern());
pattern.pattern();
for(int i = 0; i <= matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println("group " + i + " : " + groupStr);
}
System.out.println("-------------");
}

result *************
input string : abbabcd abbbabbbbcd
pattern string : (a(b*))+(c*)
group 0 : abbabc
group 1 : ab
group 2 : b
group 3 : c
-------------
input string : abbabcd abbbabbbbcd
pattern string : (a(b*))+(c*)
group 0 : abbbabbbbc
group 1 : abbbb
group 2 : bbbb
group 3 : c


3. It's not what I expected. I think this way;

(a(b*))+(c*) matches abbabc and abbbabbbbc
and when I want group(0) for the first loop, this gives abbabc.
and group(1) gives abb because quantifier after the group notation is not considered in the group(n) method.
and group(2) gives bb because the group(2) gives the first match in the previous match, in this case, abb, the result of group(1).
and group(3) gives c because the most previous match is from group(0), abbabc

and when I loop next
group(0) gives abbbabbbbc the second match.
group(1) gives abbb the first match out of the previous group(0)
group(2) gives bbb the first match out of the previous group(1)
group(3) gives c, the first match out of group(0), not group(2), nor group(1)
what's wrong?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top