Saturday, November 26, 2011

Regular expressions simplify pattern-matching code - 22


The following example demonstrates the capturing group methods:
Pattern p = Pattern.compile ("(.(.(.)))");
Matcher m = p.matcher ("abc");
m.find ();
System.out.println (m.groupCount ());
for (int i = 0; i <= m.groupCount (); i++)
     System.out.println (i + ": " + m.group (i));

The example produces the following output:
3
0: abc
1: abc
2: bc
3: c

Capturing group number 0 saves the previous match and has nothing to do with whether a capturing group appears in the pattern, which is (.(.(.))). The other three capturing groups capture the characters of the previous match belonging to those capturing groups. For example, number 2, (.(.)), captures bc; and number 3, (.), captures c.
Before we leave our discussion of Matcher's methods, we should examine four more match position methods:
  • public int start(): returns the previous match's start index. That method throws an IllegalStateException object if either the matcher has not yet attempted a match or the previous match operation failed.
  • public int start(int group): resembles the previous method, except it returns the previous match's start index associated with the capturing group that group specifies. If no capturing group with the specified capturing group number exists in the pattern, start(int group) throws an IndexOutOfBoundsException object.
  • public int end(): returns the index of the last matched character plus 1 in the previous match. That method throws an IllegalStateException object if either the matcher has not yet attempted a match or the previous match operation failed.
  • public int end(int group): resembles the previous method, except it returns the previous match's end index associated with the capturing group that group specifies. If no capturing group with the specified group number exists in the pattern, end(int group) throws an IndexOutOfBoundsException object.

No comments: