Team LiB
Previous Section Next Section

Chapter 4: Object-Oriented Regex

Overview

"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."

— Scott Adams

In this chapter, I provide some suggestions for using regular expressions in the context of a fully object-oriented language such as Java. This can, and often should, be a different experience than in more procedural languages, because Java offers a different toolset. This chapter covers when to use the java.util.regex package, when not to use it, and when to complement the java.util.regex package with other Java features.

Regular expressions, as formulated in Java, have a lot in common with the principles that guide Java Database Connectivity (JDBC). JDBC uses a generic descriptive language, Structured Query Language (SQL), for describing and manipulating information, whereas the java.util.regex package uses regular expressions. SQL provides a general Command object to "compile" that description, just as regex uses the Pattern object. SQL provides a ResultSet to return the results of that search, whereas regex uses a Matcher. The general pattern of use in both JDBC and the regex package is similar. Namely, both provide a generic description, compile it in one object, and examine the results in another object.

It only makes sense to take advantage of the relevant lessons from JDBC programming when you work with regex in Java. The combination of JDBC and general Java principles that I've found to be useful are as follows:

  • Optimize your connections.

  • Batch reads and writes.

  • Store your patterns externally.

  • Compile your Pattern as you need to.

  • Don't limit yourself to a regex solution.


Team LiB
Previous Section Next Section