TechBookReport logo

Keywords: Regular expressions, regex, Java

Title: Java Regular Expressions

Author: Mehran Habibi

Publisher: APress

ISBN: 1590591070

Media: Book

Level: Intermediate Java, beginning regular expressions

Verdict: An excellent resource on all aspects of regular expressions in Java

At long last, a book devoted to regular expressions in Java. While there are plenty of regex titles around, there is precious little devoted exclusively to the Java implementation released as part of the J2SE 1.4 release. While the Javadocs can get you so far, a solid text dealing with the more obscure ins and outs has been sorely missed in these quarters.

The subtitle of the book is 'taming the java.util.regex engine', and author Mehran Habibi does a pretty good job of putting things in their place. The opening chapter, which comes in at around 50 pages, introduces the topic with a run-down of regex in general and the Java implementation in particular. The emphasis is on lots of examples, with clear coding and an annotated explanation of the regular expressions being used. Examples, such as finding email addresses are worked through in some detail, showing how to go about creating the patterns rather than just pulling a solution out of a hat. The first chapter is a really solid introduction and tutorial, which is more than enough to get a Java programmer with no previous regex experience using the regex package to good effect.

The second chapter goes into more of the detail of the regex package, focusing on the Pattern and Matcher classes, as well as looking at the regex additions to the String class. This is more than a re-run of the package documentation, with some good examples and some sample code. The material on the use of groups is especially useful, and the illustrated examples help a great deal.

Chapter three looks at 'Advanced Regex' and delves into the arcane world of capturing and non-capturing groups, back references, qualifiers (greedy, possessive and reluctant) and at lookarounds. It's tricky stuff, but for anybody wanting to write complex but efficient regex code then this is essential. It might not be immediately useful to the average Java developer, but for those who want to get the most out of the regular expressions this is worth having around.

This is followed by a chapter on 'Object-Oriented Regex', which provides some sound advice on how to integrate regex code into fully object-oriented applications. Here the author stresses the importance of balancing the power of regular expressions with the core strengths of Java. The intention is to show that having a powerful regex tool does not mean that they should be used to crack every problem. Use them wisely and get the most out of them is the core message.

Having covered all of the core concepts and methods, the book moves on in chapter five to look at some detail at a number of extended practical examples. These include phone number formats, zip codes, date formats, searching a string or a file and so on. As with many of the other examples in the book, the author does not simply produce a finished solution but works through a process to show how to build your own patterns.

Finally, the appendices provide some additional reference material , concluding with a set of common useful patterns.

What's missing from the book is much in the way of a conceptual view of regular expressions - in other words any discussion of finite state machines (or finite automata). This isn't really a problem from practical point of view, but it does help to make clear how regular expressions work. Anyone looking for a good article on this is directed to the article 'How Regexes Work' by Mark Jason Dominus, reprinted in Computer Science and Perl Programming.

However, this aside, the book does provide a very firm grounding in all aspects of regular expressions in Java. If you have Java experience but are pretty new to regular expressions, or want to go further than the basics, then this is an excellent resource.

Hit the 'back' key in your browser to return to subject index page

Return to home page