Chapter 8. Java


Java has had a native regex package, java.util.regex , since the early-2002 release of Java 1.4.0. It provides powerful and innovative functionality with an uncluttered (if somewhat simplistic) API. It has fairly good Unicode support, clear documentation, and fast execution. It matches against CharSequence objects, so it can be quite flexible in its application.

The original release of java.util.regex was impressive. Its feature set, speed, and relative lack of bugs were all of the highest caliber, especially considering that it was an initial release. The final 1.4 release was Java 1.4.2. As of this writing, Java 1.5.0 (also called Java 5.0) has been released, and Java 1.6.0 (also called Java 6.0 and "Mustang") is in its second beta release. Officially, this book covers Java 1.5.0, but I'll note important differences from Java 1.4.2 and the second 1.6 beta where appropriate. (The differences are also summarized at the end of this chapter ˜ 401.) [ ]

[ ] This book covers Java 1.5.0 as of Update 7. The initial release of Java 1.5 had a severe bug related to case-insensitive matches. It was fixed in Update 2, so I recommend upgrading if youre running a prior version. The Java 1.6 beta information is current as of the second beta release, build 59g.

Reliance on Earlier Chapters Before looking at what's in this chapter, it's important to mention that it doesn't restate everything from Chapters 1 through 6. I understand that some readers interested only in Java may be inclined to start their reading with this chapter, and I want to encourage them not to miss the benefits of the preface and the earlier chapters: Chapters 1, 2, and 3 introduce basic concepts, features, and techniques involved with regular expressions, while Chapters 4, 5, and 6 offer important keys to regex understanding that apply directly to java.util.regex . Among the important concepts covered in earlier chapters are the base mechanics of how an NFA regex engine goes about attempting a match, greediness , backtracking, and efficiency concerns.

Table 8-1. Index of Methods

Sorted alphabetically , with page numbers

380 appendReplacement

373 matcher

379 replaceFirst

381 appendTail

376 matches (Matcher)

390 requireEnd

372 compile

395 matches (Pattern)

392 reset

377 end

393 pattern (Matcher)

395 split

375 find

394 pattern (Pattern)

377 start

394 flags

395 quote

394 text

377 group

379 quoteReplacement

377 toMatchResult

377 groupCount

386 region

393 toString (Matcher)

388 hasAnchoringBounds

386 regionEnd

394 toString (Pattern)

387 hasTransparentBounds

386 regionStart

388 useAnchoringBounds

390 hitEnd

378 replaceAll

393 usePattern

376 lookingAt

382 replaceAllRegion

387 useTransparentBounds

This table has been placed here for easy reference. The regex API discussion starts on page 371 .


Along those lines, let me emphasize that despite convenient tables such as the one in this chapter on page 367, or, for example, ones in Chapter 3 such as those on pages 114 and 123, this book's foremost intention is not to be a reference, but a detailed instruction on how to master regular expressions.

We've seen examples of java.util.regex in earlier chapters (˜ 81, 95, 98, 217, 235), and we'll see more in this chapter when we look at its classes and how to actually put it to use. First, though, we'll take a look at the regex flavor it supports, as well as the modifiers that influence that flavor.



Mastering Regular Expressions
Mastering Regular Expressions
ISBN: 0596528124
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net