Home arrow Java arrow Page 5 - Regular Expressions

Regular Expressions

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 28
July 28, 2005
  1. · Regular Expressions
  2. · Creating Patterns
  3. · Common and Boundary Characters
  4. · Character Classes
  5. · Back References
  6. · Integrating Java with Regular Expressions
  7. · Confirming Name Formats Example
  8. · Finding Duplicate Words Example
  9. · Regular Expression Operations
  10. · Search and Replace
  11. · Comparing Regex and Perl

print this article

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


The 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


Close group


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.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials