没有合适的资源?快使用搜索试试~ 我知道了~
首页编程宝典:精通正则表达式解决问题
编程宝典:精通正则表达式解决问题
需积分: 10 30 下载量 93 浏览量
更新于2024-07-20
收藏 2.9MB PDF 举报
《正则表达式Cookbook》是一本由O'Reilly出版的专业书籍,专门针对软件开发和编程中的正则表达式应用提供详尽的指南。这本书涵盖了超过100个实用的正则表达式配方,旨在帮助开发者高效地处理和操作文本数据,解决在各种编程语言中遇到的常见问题。 作者深入浅出地介绍了正则表达式的基础知识,通过简短的教程让读者快速掌握核心概念,无论是初学者还是经验丰富的用户都能从中受益。书中特别关注了如何在诸如C、Java、JavaScript、Perl、PHP、Python、Ruby、Indigo和VB.NET等主流编程语言中有效地运用正则表达式,确保读者能够熟练运用它们进行输入验证、格式化处理、解析语言结构、处理特殊字符、数值以及在URLs、HTML、XML和数据交换等场景中找到解决方案。 该书不仅介绍了基本的正则表达式语法和用法,还深入探讨了更高级特性的运用,以及不同编程语言中正则表达式语法和行为的异同。通过阅读这本书,开发者不仅能提升自身的技能,还能避免常见的性能瓶颈、错误匹配和困扰,从而提高代码的稳定性和可维护性。 《正则表达式Cookbook》是一本全面而实用的工具书,无论你是想要入门正则表达式,还是寻求提升现有技能,它都是一个不可或缺的参考资料。通过本书,读者将能够在各种实际项目中得心应手地利用正则表达式进行高效的数据处理和文本分析。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/9856757/bg10.jpg)
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book where we list errata, examples, and any additional
information. You can access this page at:
http://www.regexcookbook.com
or at:
http://oreilly.com/catalog/9780596520687
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com
For more information about our books, conferences, Resource Centers, and the
O’Reilly Network, see our website at:
http://www.oreilly.com
Acknowledgments
We thank Andy Oram, our editor at O’Reilly Media, Inc., for helping us see this project
from start to finish. We also thank Jeffrey Friedl, Zak Greant, Nikolaj Lindberg, and
Ian Morse for their careful technical reviews, which made this a more comprehensive
and accurate book.
xiv | Preface
![](https://csdnimg.cn/release/download_crawler_static/9856757/bg11.jpg)
CHAPTER 1
Introduction to Regular Expressions
Having opened this cookbook, you are probably eager to inject some of the ungainly
strings of parentheses and question marks you find in its chapters right into your code.
If you are ready to plug and play, be our guest: the practical regular expressions are
listed and described in Chapters 4 through 8.
But the initial chapters of this book may save you a lot of time in the long run. For
instance, this chapter introduces you to a number of utilities—some of them created
by one of the authors, Jan—that let you test and debug a regular expression before you
bury it in code where errors are harder to find. And these initial chapters also show you
how to use various features and options of regular expressions to make your life easier,
help you understand regular expressions in order to improve their performance, and
learn the subtle differences between how regular expressions are handled by different
programming languages—and even different versions of your favorite programming
language.
So we’ve put a lot of effort into these background matters, confident that you’ll read it
before you start or when you get frustrated by your use of regular expressions and want
to bolster your understanding.
Regular Expressions Defined
In the context of this book, a regular expression is a specific kind of text pattern that
you can use with many modern applications and programming languages. You can use
them to verify whether input fits into the text pattern, to find text that matches the
pattern within a larger body of text, to replace text matching the pattern with other
text or rearranged bits of the matched text, to split a block of text into a list of subtexts,
and to shoot yourself in the foot. This book helps you understand exactly what you’re
doing and avoid disaster.
1
![](https://csdnimg.cn/release/download_crawler_static/9856757/bg12.jpg)
History of the Term ‘Regular Expression’
The term regular expression comes from mathematics and computer science theory,
where it reflects a trait of mathematical expressions called regularity. Such an expres-
sion can be implemented in software using a deterministic finite automaton (DFA). A
DFA is a finite state machine that doesn’t use backtracking.
The text patterns used by the earliest grep tools were regular expressions in the math-
ematical sense. Though the name has stuck, modern-day Perl-style regular expressions
are not regular expressions at all in the mathematical sense. They’re implemented with
a nondeterministic finite automaton (NFA). You will learn all about backtracking
shortly. All a practical programmer needs to remember from this note is that some ivory
tower computer scientists get upset about their well-defined terminology being over-
loaded with technology that’s far more useful in the real world.
If you use regular expressions with skill, they simplify many programming and text
processing tasks,
and allow many that wouldn’t be at all feasible without the regular
expressions. You would need dozens if not hundreds of lines of procedural code to
extract all email addresses from a document—code that is tedious to write and hard to
maintain. But with the proper regular expression, as shown in Recipe 4.1, it takes just
a few lines of code, or maybe even one line.
But if you try to do too much with just one regular expression, or use regexes where
they’re not really appropriate, you’ll find out why some people say:
*
Some people, when confronted with a problem, think “I know, I’ll use regular expres-
sions.” Now they have two problems.
The second problem those people have is that they didn’t read the owner’s manual,
which you are holding now. Read on. Regular expressions are a powerful tool. If your
job involves manipulating or extracting text on a computer, a firm grasp of regular
expressions will save you plenty of overtime.
Many Flavors of Regular Expressions
All right, the title of the previous section was a lie. We didn’t define what regular
expressions are. We can’t. There is no official standard that defines exactly which text
patterns are regular expressions and which aren’t. As you can imagine, every designer
of programming languages and every developer of text processing applications has a
different idea of exactly what a regular expression should be. So now we’re stuck with
a whole palate of regular expression flavors.
Fortunately, most designers and developers are lazy. Why create something totally new
when you can copy what has already been done? As a result, all modern regular ex-
pression flavors, including those discussed in this book, can trace their history back to
*
Jeffrey Friedl traces the history of this quote in his blog at http://regex.info/blog/2006-09-15/247.
2 | Chapter 1: Introduction to Regular Expressions
![](https://csdnimg.cn/release/download_crawler_static/9856757/bg13.jpg)
the Perl programming language. We call these flavors Perl-style regular expressions.
Their regular expression syntax is very similar, and mostly compatible, but not com-
pletely so.
Writers are lazy, too. We’ll usually type regex or regexp to denote a single regular
expression, and regexes to denote the plural.
Regex flavors do not correspond one-to-one with programming languages. Scripting
languages tend to have their own, built-in regular expression flavor. Other program-
ming languages rely on libraries for regex support. Some libraries are available for mul-
tiple languages, while certain languages can draw on a choice of different libraries.
This introductory chapter deals with regular expression flavors only and completely
ignores any programming considerations. Chapter 3 begins the code listings, so you
can peek ahead to “Programming Languages and Regex Flavors” in Chapter 3 to find
out which flavors you’ll be working with. But ignore all the programming stuff for now.
The tools listed in the next section are an easier way to explore the regex syntax through
“learning by doing.”
Regex Flavors Covered by This Book
For this book, we selected the most popular regex flavors in use today. These are all
Perl-style regex flavors. Some flavors have more features than others. But if two flavors
have the same feature, they tend to use the same syntax. We’ll point out the few an-
noying inconsistencies as we encounter them.
All these regex flavors are part of programming languages and libraries that are in active
development. The list of flavors tells you which versions this book covers. Further along
in the book, we mention the flavor without any versions if the presented regex works
the same way with all flavors. This is almost always the case. Aside from bug fixes that
affect corner cases, regex flavors tend not to change, except to add features by giving
new meaning to syntax that was previously treated as an error:
Perl
Perl’s built-in support for regular expressions is the main reason why regexes are
popular today. This book covers Perl 5.6, 5.8, and 5.10.
Many applications and regex libraries that claim to use Perl or Perl-compatible
regular expressions in reality merely use Perl-style regular expressions. They use a
regex syntax similar to Perl’s, but don’t support the same set of regex features.
Quite likely, they’re using one of the regex flavors further down this list. Those
flavors are all Perl-style.
PCRE
PCRE is the “Perl-Compatible Regular Expressions” C library developed by Philip
Hazel. You can download this open source library at http://www.pcre.org. This
book covers versions 4 through 7 of PCRE.
Regular Expressions Defined | 3
![](https://csdnimg.cn/release/download_crawler_static/9856757/bg14.jpg)
Though PCRE claims to be Perl-compatible, and probably is more than any other
flavor in this book, it really is just Perl-style. Some features, such as Unicode sup-
port, are slightly different, and you can’t mix Perl code into your regex, as Perl itself
allows.
Because of its open source license and solid programming, PCRE has found its way
into many programming languages and applications. It is built into PHP and wrap-
ped into numerous Delphi components. If an application claims to support “Perl-
compatible” regular expressions without specifically listing the actual regex flavor
being used, it’s likely PCRE.
.NET
The Microsoft .NET Framework provides a full-featured Perl-style regex flavor
through the System.Text.RegularExpressions package. This book covers .NET
versions 1.0 through 3.5. Strictly speaking, there are only two versions of
System.Text.RegularExpressions: 1.0 and 2.0. No changes were made to the Regex
classes in .NET 1.1, 3.0, and 3.5.
Any .NET programming language, including C#, VB.NET, Delphi for .NET, and
even COBOL.NET, has full access to the .NET regex flavor. If an application de-
veloped with .NET offers you regex support, you can be quite certain it uses
the .NET flavor, even if it claims to use “Perl regular expressions.” A glaring ex-
ception is Visual Studio (VS) itself. The VS integrated development environment
(IDE) still uses the same old regex flavor it has had from the beginning, which is
not Perl-style at all.
Java
Java 4 is the first Java release to provide built-in regular expression support through
the java.util.regex package. It has quickly eclipsed the various third-party regex
libraries for Java. Besides being standard and built in, it offers a full-featured Perl-
style regex flavor and excellent performance, even when compared with applica-
tions written in C. This book covers the java.util.regex package in Java 4, 5, and
6.
If you’re using software developed with Java during the past few years, any regular
expression support it offers likely uses the Java flavor.
JavaScript
In this book, we use the term JavaScript to indicate the regular expression flavor
defined in version 3 of the ECMA-262 standard. This standard defines the
ECMAScript programming language, which is better known through its JavaScript
and JScript implementations in various web browsers. Internet Explorer 5.5
through 8.0, Firefox, Opera, and Safari all implement Edition 3 of ECMA-262.
However, all browsers have various corner case bugs causing them to deviate from
the standard. We point out such issues in situations where they matter.
If a website allows you to search or filter using a regular expression without waiting
for a response from the web server, it uses the JavaScript regex flavor, which is the
4 | Chapter 1: Introduction to Regular Expressions
剩余511页未读,继续阅读
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/3e9e322856014c21ad5ea1efd4a68742_turbo0708.jpg!1)
turbo0708
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 多模态联合稀疏表示在视频目标跟踪中的应用
- Kubernetes资源管控与Gardener开源软件实践解析
- MPI集群监控与负载平衡策略
- 自动化PHP安全漏洞检测:静态代码分析与数据流方法
- 青苔数据CEO程永:技术生态与阿里云开放创新
- 制造业转型: HyperX引领企业上云策略
- 赵维五分享:航空工业电子采购上云实战与运维策略
- 单片机控制的LED点阵显示屏设计及其实现
- 驻云科技李俊涛:AI驱动的云上服务新趋势与挑战
- 6LoWPAN物联网边界路由器:设计与实现
- 猩便利工程师仲小玉:Terraform云资源管理最佳实践与团队协作
- 类差分度改进的互信息特征选择提升文本分类性能
- VERITAS与阿里云合作的混合云转型与数据保护方案
- 云制造中的生产线仿真模型设计与虚拟化研究
- 汪洋在PostgresChina2018分享:高可用 PostgreSQL 工具与架构设计
- 2018 PostgresChina大会:阿里云时空引擎Ganos在PostgreSQL中的创新应用与多模型存储
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)