Professional Documents
Culture Documents
Database in Action
Database in Action
Database in Action
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information
storage or retrieval system, without the prior written permission of the copyright owner and
the publisher.
ISBN 978-1-59059-107-9 ISBN 978-1-4302-0709-2 (eBook)
DOI 10.1007/978-1-4302-0709-2
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Technical Reviewer: Bill Saez
Editorial Board: Steve Anglin, Dan Appleman, Gary Cornell, James Cox, Tony Davis,
John Franklin, Chris Mills, Steven Rycroft, Dominic Shakeshaft, Julian Skinner, Jim Sumser,
Karen Watterson, Gavin Wray, John Zukowski
Assistant Publisher: Grace Wong
Project Manager: Nate McFadden
Copy Editor: Nicole LeClerc
Production Manager: Kari Brooks
Production Editor: Laura Cheu
Proofreader: linda Seifert
Compositor: Susan Glinert Stevens
Indexer: Kevin Broccoli
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
The information in this book is distributed on an "as is" basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at http: I /Wv~t~.apress.com in the Downloads
section. You will need to answer questions pertaining to this book in order to successfully
download the code.
This book is dedicated to my lovely wife, Angela Young, MD.
I must have been really, really good in a previous life.
Contents at a Glance
v
Contents
Introduction .................................................................................................... xv
vii
Contents
viii
About the Author
Mehran Habibi is the coauthor of The Sun Certified
Java Developer Exam with ]2SE 1.4 (Apress, 2003)
and Cracking the AP Computer Science Exam,
2004-2005 Edition (Princeton Review, 2004). He is
also an application architect with BankOne in Ohio,
where he resides with his lovely wife, Angela. Mehran
has over nine years of IT experience, including
positions with IBM, Executive Jet, UUNET, BankOne,
and OCLC, in addition to working as a university
lecturer, independent consultant, and Java certifi-
cation trainer. Technologies of interest to him include Web services, wireless
technologies, and XML/XSIT. Mehran's professional focus has been on architecture,
project leadership, mento ring, team leadership, and programming from the mid -tier
on back. Mehran holds certifications in both "The Other Company" and Java 2, and
he graduated with a bachelor's of science degree in software engineering from the
honors program at The Ohio State University.
Mehran is an amateur boxer, teaches martial arts at The Ohio State University,
enjoys soccer, and has ruined his chess by playing too many speed games. You can
contact him at coach@influxs. com.
ix
About the
Technical Reviewer
Bill Saez is a software engineer with Motorola in Ft. Lauderdale, Florida. While
working with Motorola, Bill helped to create the world's first Java-powered wireless
handset with J2ME and CLDC certification in 2000. Since then, he has continued
to play an integral part in the commercialization and development of the J2ME
platform and has authored several OEM APis for iDEN handsets as well as J2ME
Developer Guides for those products. Bill has been involved with Java development
since its introduction and even served as a guinea pig for The Ohio State University's
experimental Java software courses. He received his bachelor's degree in software
engineering from The Ohio State University and is currently pursuing a master's
degree in computer science from the University of Florida.
When he's not working or studying, Bill enjoys training for and running
marathons, traveling with his family, and infrequently writing game reviews
(http: I lwww. epinions. com/user-billservo) in his copious spare time.
xi
Acknowledgments
I'D UKE TO THANK Nate McFadden, Gary Cornell, Nicole LeClerc, and Laura Cheu
from Apress for being such a joy to work with. I'd also like to acknowledge the
strong contributions of various friends, including Terry Camerlengo, the excellent
people at JavaRanch (http: I /www.javaranch. com), and various kind others who
provided feedback and suggestions. In particular, I'm grateful for Jim Yingst's fine
critical eye. I would also like to acknowledge the strong mathematical analysis
provided by my father, Dr. Javad Habibi. Last but certainly not least, I'd like to
thank my technical reviewer, Bill Saez, for an amazing technical eye and a very
gentle style. I can't wait to see your book there, Bill.
xiii
Introduction
THE FUNDAMENTAL GOAL of any computer language is the manipulation of data.
Traditionally, Java has been an excellent language for doing so, provided that the
data is represented as objects. However, Java's raw data manipulation mechanisms
have always been somewhat lacking, especially when compared to the powerful
machinations offered by languages such as Perl and awk.
The introduction of a standard regular expression package into Java 2 Standard
Edition (J2SE) is an excellent step in rectifying this oversight. The java. util. regex
package offers developers everything they need to use regular expressions in Java,
all packaged in an easy-to-use, object-oriented structure. I think that you'll find
that the java. util. regex package can become an extremely powerful tool in your
programming arsenal, as well an elegant instrument that you'll enjoy using. After
you've mastered it, you will wonder, as I did, how you ever managed without it.
and Bert Bates (O'Reilly & Associates, 2003) and Thinking in Java, Third Edition by
Bruce Eckel (Prentice Hall, 2002). You can't go wrong with either book.
Chapter 1
This chapter introduces regular expressions and provides some simple examples
and explanations to get you started. It explores the J2SE regular expression syntax,
operations, and differences from regular expressions you might already be familiar
with from other languages. It also offers a tutorial on regular expressions.
Even if you have a background in regular expressions, I suggest you look over
the examples in this chapter-there are a lot of different regular expression flavors,
and there's rarely an isomorphic mapping between them. Chapter 1 is a natural
starting point if you're new to regular expressions in J2SE or if you need a refresher
on regular expressions in general.
Chapter 2
Chapter 2 introduces the built-in Java support for regular expressions through the
Pattern and Matcher classes. Each method and attribute is dissected in detail, and
all but the most trivial have companion code examples that highlight appropriate
usage. Chapter 2 covers which settings and flags might affect the efficiency of
your code. Additionally, Chapter 2 details the five new methods on the String
class that support regular expressions, and offers advice and examples regarding
appropriate usage.
Chapter 3
Chapter 3 expounds on some advanced regular expression concepts, including
groups, noncapturing groups, greedy qualifiers, positive qualifiers, reluctant qual-
ifiers, possessive qualifiers, positive lookarounds, negative lookarounds, positive
lookbehinds, and negative lookbehinds. It provides numerous examples and
detailed explanations regarding how these concepts work and how they might
benefit you in your day-to-day tasks.
xvi
Introduction
Chapter 4
Chapter 4 offers advice and suggestions on using regular expressions in Java's
object-oriented environment. The chapter covers best practices, examples, and
lessons learned from similar packages.
Chapter 5
Chapter 5 provides numerous full-featured examples, with accompanying expla-
nations and code, that build on the material presented in previous chapters.
Chapter 5 is designed to illustrate the development process I use when I'm trying
to solve a regular expression problem in Java.
Appendix A
Appendix A offers a complete listing, with explanation, of the regular expression
syntax and various regular expression metacharacters.
Appendix 8
Appendix B provides a summary of the methods of the Pattern and Matcher classes
in Java.
Appendix C
Appendix C offers simple regular expressions, without accompanying code, to
help with everyday, common tasks such as validating e-mails, checking text for
format, extracting values, and so on.
xvii