Chapter 5. Regular Expressions

Hey, it’s a kind of magic.

The Highlander

In this chapter, we’ll discuss the API of the classes in the new java.util.regex package (see Figure 5-1). JSR 51, the Java Specification Request defining the new I/O capabilities, also specifies the addition of regular expression processing to the Java platform. While regular expressions, strictly speaking, are not I/O, they are most commonly used to scan text data read from files or streams.

The regular expression classes
Figure 5-1. The regular expression classes

You’ll learn how to use the new Java APIs to do the same powerful pattern matching that has been available to users of perl, egrep, and other text-processing tools. A detailed exploration of regular expression syntax is beyond the scope of this book, but a working familiarity with regular expressions is assumed. If you’re new to regular expressions, want to improve your skills, or are baffled by this chapter, I recommend you pick up a good reference. O’Reilly publishes an authoritative regular expression book (it’s even cited in the JDK documentation): Mastering Regular Expressions, by Jeffrey E. F. Friedl (http://www.oreilly.com/catalog/regex/).

Regular Expression Basics

A regular expression is a sequence of characters that describe, or express, a pattern of characters you are interested in matching within a target character sequence. Regular expressions have been widely available ...

Get Java NIO now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.