regular_expressions_cookbook.pdf

(10822 KB) Pobierz
SECOND EDITION
Regular Expressions Cookbook
Jan Goyvaerts and Steven Levithan
Beijing
Cambridge
Farnham
Köln
Sebastopol
Tokyo
Regular Expressions Cookbook, Second Edition
by Jan Goyvaerts and Steven Levithan
Copyright © 2012 Jan Goyvaerts, Steven Levithan. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editor:
Andy Oram
Production Editor:
Holly Bauer
Copyeditor:
Genevieve d’Entremont
Proofreader:
BIM Publishing Services
August 2012:
Second Edition.
Indexer:
BIM Publishing Services
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrator:
Rebecca Demarest
Revision History for the Second Edition:
2012-08-10
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9781449319434
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc.
Regular Expressions Cookbook,
the image of a musk shrew, and related trade dress
are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-1-449-31943-4
[LSI]
1344629030
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Introduction to Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Regular Expressions Defined
Search and Replace with Regular Expressions
Tools for Working with Regular Expressions
1
6
8
2. Basic Regular Expression Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13
2.14
2.15
2.16
2.17
2.18
2.19
2.20
2.21
2.22
Match Literal Text
Match Nonprintable Characters
Match One of Many Characters
Match Any Character
Match Something at the Start and/or the End of a Line
Match Whole Words
Unicode Code Points, Categories, Blocks, and Scripts
Match One of Several Alternatives
Group and Capture Parts of the Match
Match Previously Matched Text Again
Capture and Name Parts of the Match
Repeat Part of the Regex a Certain Number of Times
Choose Minimal or Maximal Repetition
Eliminate Needless Backtracking
Prevent Runaway Repetition
Test for a Match Without Adding It to the Overall Match
Match One of Two Alternatives Based on a Condition
Add Comments to a Regular Expression
Insert Literal Text into the Replacement Text
Insert the Regex Match into the Replacement Text
Insert Part of the Regex Match into the Replacement Text
Insert Match Context into the Replacement Text
28
30
33
38
40
45
48
62
63
66
68
72
75
78
81
84
91
93
95
98
99
103
iii
Zgłoś jeśli naruszono regulamin