Regular expressions are a mechanism for telling the Java Virtual Machine (JVM) how to find and manipulate text for you. Using regular expressions to do this is different from the traditional approach. This article compares the two approaches. It is excerpted from Java Regular Expressions: Taming the java.util.regex Engine, written by Mehran Habibi (Apress, 2004; ISBN: 1590591070).
Regular Expressions - Back References (Page 5 of 11 )
Back references are one of the most powerful features offered by regular expressions. Unfortunately, programmers often skip over them because theyíre not explained well in the regular expression literature. Thatís a mistake I hope to rectify here.
Back references allow a pattern to refer back to parts of itself. They always refer back to groups that were enclosed by the ď(Ē and the ď)Ēcharacters. Table 1-17 presents the syntax for back references.
Table 1-17. Back References
The first group in the pattern
The second group in the pattern
nth group in the pattern
NOTE There are some idiosyncratic behaviors associated with how back references work in Java, which I explain later in this chapter and in Chapter 3. For right now, you have enough information on back references to get started.
Back References Example
Say you need to find matches in which a word is duplicated. That is, you donít know what the word youíre looking for is, but you want to be alerted when the same word is repeated twice in a row. If youíve used a word processor such as Microsoft Word, youíll notice that the application does this automatically. Letís explore how you might do this in Java.
Youíll use the pattern \b(\w+) \1\b, which is dissected in Table 1-18. This pattern matches pizza pizza, Faster pussycat kill kill, or Never Never Never Never Never because each contains a word thatís immediately repeated. It wonít match 222 2222, sara sarah, or Faster pussycat kill, kill because these donít contain a word thatís immediately repeated. The latter group wonít match because 222 2222has a lingering 2 in the second set, sara sarah has a lingering h in the second word, and in Faster pussycat kill, kill the second kill is separated from the first by a comma.
Table 1-18. The Pattern face="courier new, courier, mono" size=2>\b(\w+) \1\b
A word boundary
Followed by a group consisting of
Any alphanumeric character
Repeated one for more times
Followed by a space
Followed by the exact group of characters captured previously a
Followed by a word boundary
In English: Look for a word boundary, followed by a group of alphanumeric characters, followed by a space, followed by the exact same group of alphanumeric characters found previously, followed by a word boundary. In short, look for duplicate words.
In the next section, youíll examine some practical examples with corresponding Java code.