Quick Start
Tools & Languages
Book Reviews
Book Reviews
Regular Expr. Cookbook
Teach Yourself Reg. Expr.
Mastering Regular Expr.
Java Regular Expressions
Oracle Regular Expr.
Regular Expr. Pocket Ref.
Regular Expr. Recipes
Regex Recipes for Windows
More on This Site
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—The best regex editor and tester for Java developers!

Java Regular Expressions by Mehran Habibi (Apress)

The book “Java Regular Expressions” consists of 250 pages of wide-spaced text, split into 5 chapters. The first chapter is an introduction to regular expressions, explaining basic regular expression syntax. The book’s pace is slower than the tutorial on this website. It doesn’t explain many of the “gotcha’s” or differences between regex flavors. Clearly, this book is aimed at people who are relatively new to regular expressions, and only plan to use them with Java.

The second chapter is the most useful part of the book. It describes all the classes in the java.util.regex package, and nearly all of their methods. Most of the descriptions come with example Java source code illustrating its use. If you prefer to learn by reading Java source code rather than English, you’ll appreciate these code snippets.

The third chapter explains advanced regular expression syntax, such as lazy and possessive quantifiers (called “qualifiers” in the book), and lookaround. Unfortunately, the quality of the book starts to go downhill from here. Minor errors such as using alternation (I|i)ce instead of a far more efficient character class [Ii]ce (page 104) or stating that \d represents a digit in the replacement text (page 107; \d only works in the regex, not in the replacement) could be attributed to sloppy editing. But the example that was intended to explain positive lookahead (page 130) is simply wrong. The regex (?=^255).* does exactly what ^255.* does. The crucial aspect of lookahead (it doesn’t consume the text it matches) is completely irrelevant in this example.

The fourth chapter could have easily been omitted from the book. It talks more about object-oriented programming techniques than about regular expressions. Some of the advice is questionable. The author suggests storing regular expressions in external files, so they can be edited without recompiling the application. The problem with this advice is that the regular expressions will then sit in an external file without context, making them much harder to understand. It contradicts the books key selling point: most concepts are explained with regexes used in Java code. If another programmer has to maintain the code later, a better idea is to use a tool such as RegexBuddy to insert a detailed English description as a Java comment into the Java source code (RegexBuddy generates them on the Use tab).

The fifth chapter presents you with several more elaborate examples performing a number of real-world tasks with regular expressions in Java. While the examples are relatively simple, they do bring everything together nicely.

Should you buy this book? While it’s definitely not perfect, I would recommend this book to Java developers who are experienced with Java, but have little or no experience with regular expressions. The book’s description of the java.util.regex package is far more detailed than documentation included with the JDK. But to learn the ins and outs of the regular expression syntax itself, you’ll need to complement this book with another resource, such as the tutorial on this website.