Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Regular expressions simplify pattern-matching code

Discover the elegance of regular expressions in text-processing scenarios that involve pattern matching

  • Print
  • Feedback

Page 15 of 16

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.


The following example demonstrates two of the match position methods reporting start/end match positions for capturing group number 2:

Pattern p = Pattern.compile ("(.(.(.)))");
Matcher m = p.matcher ("abcabcabc");
while (m.find ())
{
   System.out.println ("Found " + m.group (2));
   System.out.println ("  starting at index " + m.start (2) +
                       " and ending at index " + m.end (2));
   System.out.println ();
}


The example produces the following output:

  • Print
  • Feedback

Resources