没有合适的资源?快使用搜索试试~ 我知道了~
首页快速掌握编程技巧:正则表达式代码参考
快速掌握编程技巧:正则表达式代码参考
需积分: 10 5 下载量 138 浏览量
更新于2024-07-18
收藏 1.96MB PDF 举报
"《正则表达式快速语法参考》是一本针对编程中重要技术——正则表达式的精炼代码和语法指南。作者Zsolt Nagy编写的这本书旨在为开发者提供一个结构清晰、易于查阅的参考资料,它涵盖了多种编程语言,如JavaScript、Python、Java和C#,帮助读者掌握如何使用正则表达式进行输入验证、字符串分割、解析数据以及模式匹配。 书中特色鲜明,以简短且聚焦的代码示例呈现,避免了冗长的历史背景或故事叙述,注重实用性和可访问性。内容包括但不限于:验证用户输入的有效性,处理搜索与替换操作,以及在后台编码中过滤和处理数据。对于希望提高编程技能特别是处理文本数据的程序员来说,这是一本不可或缺的工具书。 该书的ISBN-13平装版为978-1-4842-3875-2,电子版为978-1-4842-3876-9,且已获得版权保护,由Springer International Publishing出版,并获得了美国国会图书馆的控制号2018953563。所有内容受版权法保护,禁止任何形式的未经许可复制、传播或改编,包括但不限于电子化、计算机软件利用等。 书中还可能包含商标名、标志和图片,这些使用遵循编辑性使用原则,不需每次出现都标注商标符号。《正则表达式快速语法参考》是为高效学习和实际工作中处理文本处理任务的专业程序员量身打造的权威指南。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/10683187/bg10.jpg)
6
By definition, regexes specify a search pattern. Although this is a true
statement, it is easy to misinterpret it because you are not specifying
a declarative structure. In the real world, you specify a sequence of
instructions acting like a function in an imperative programming language.
You use commands and loops, you pass arguments to your regex, you may
pass arguments around inside your regex, you return a result, and you may
even cause side effects.
Learning regular expressions as an imperative language comes with a
big advantage. If you have dealt with at least one programming language in
your life, chances are, you know almost everything to understand regular
expressions. You are just not yet proficient in the regex syntax. As soon as you
familiarize yourself with this weird language, everything will fall into place.
The Language Family ofRegular
Expressions
When I talk about regular expressions, in practice I mean a family of
different dialects. Similarly to genetics, regular expressions keep evolving,
and new mutations surface on a regular basis. Although the principles stay
the same in most languages, every single dialect brings something different.
Standardization of regular expressions began with BRE (Basic Regular
Expressions) inside the POSIX standard 1003.2. This standard is used in
the editors ed and sed, as well as in the grep command.
The first major evolution of regular expressions came with the ERE
(Extended Regular Expressions) syntax. This syntax is used in, for example,
egrep and notepad++.
For completeness, I will also mention the SRE (Simple Regular
Expressions) dialect, which has been deprecated in favor of BRE.
Some editors such as EMACS and VIM have their own dialects. In the
case of VIM, the dialect can be customized with flags, and this technique
provides even more variations. These dialects are built on top of ERE.
CHAPTER 1 AN INTRODUCTION TO REGULAR EXPRESSIONS
![](https://csdnimg.cn/release/download_crawler_static/10683187/bg11.jpg)
7
The regular expressions used in most programming languages are
based on the PCRE (Perl Compatible Regular Expressions) dialect. Each
programming language has its own abbreviations and differences. These
programming languages include PHP, JavaScript, Java, C#, C++, Python, R,
Perl up to version 5, and more.
To make matters more complex, Perl 6 comes with a completely
different set of rules for regular expressions. The Perl 6 syntax is often
easier to read, but in exchange, you have to learn a different language.
As an example, let’s write a regex for matching strings that contain at
least one non-numeric character.
Dialect Expression
BRE, ERE, EMACS, VIM, PCRE /[^0123456789]/
Perl 6 /<-[0123456789]>/
As you can see, in this specific example, all dialects but Perl 6 look
identical. Without getting lost in the details too much, let’s understand
what this expression means in BRE, ERE, EMACS, VIM, and PCRE.
– [0123456789] matches one single character from
the character set of digits.
– ^ inside an enumeration negates the character list.
is means [^0123456789] matches any character
that’s not a digit.
– As the regular expression may match any character
of the test string, a match is determined as soon as
you find at least one character in the test string that’s
not a digit. Therefore, 123.45 matches the regular
expression, while 000 does not.
The Perl 6 syntax works in the same way; the syntax is just different.
CHAPTER 1 AN INTRODUCTION TO REGULAR EXPRESSIONS
![](https://csdnimg.cn/release/download_crawler_static/10683187/bg12.jpg)
8
Let’s now write a regular expression that matches the 0, 1, or 2
character, using the or operator of regular expressions.
BRE: or operator is not supported
ERE,PCRE,Perl 6: /0|1|2/
EMACS,VIM: /0\|1\|2/
An equivalent BRE expression would be /[012]/, using a character set.
You will study character sets in detail in Chapter 6.
As studying six groups and many different variations would take a long
time, I highly recommend you stick to one specific dialect and practice
your skills focusing on the one and only dialect you use in practice. You
can come back to study other dialects later. When it comes to the PCRE
dialect, different languages give you different variations. I have personally
found it beneficial to build and execute regular expressions in multiple
programming languages. This way, I had an easier time solidifying my
regex knowledge from different angles.
Summary
In this chapter, I defined a regular expression as a finite character
sequence defining a search pattern. As an example, you saw a test
execution of a simple JavaScript regular expression in the console.
Although the tested regular expression was simple, oftentimes people have
a hard time constructing and understanding regular expressions. This is
because regular expressions represent a compact imperative language,
and therefore, they are often not intuitive to understand. To make matters
more complicated, regular expressions consist of multiple languages,
which means that the JavaScript syntax is completely different than the
syntax used in Perl 6.
CHAPTER 1 AN INTRODUCTION TO REGULAR EXPRESSIONS
![](https://csdnimg.cn/release/download_crawler_static/10683187/bg13.jpg)
9
© Zsolt Nagy 2018
Z. Nagy, Regex Quick Syntax Reference, https://doi.org/10.1007/978-1-4842-3876-9_2
CHAPTER 2
Regex Syntax 101
When learning a new tool, you always have to get started somewhere.
The goal of this chapter is to give you a basic subset of the regular
expression syntax to play with. Learning all the syntax is not productive,
though, because let’s face it, learning advanced regular expression syntax
all at once is too much for anyone.
Formulating anExpression
A regular expression is written inside a starting slash and an ending slash
character: /re/.
As you saw in Chapter 1, this expression matches strings containing re.
Some programming languages allow you or require you to use a
different notation. For instance, in JavaScript, you may use the following
form to define the regular expression /re/:
> const regex = new RegExp( 're' );
> regex
/re/
In PHP and Python, you have to use strings or raw strings to pass a
regular expression to a function. Here’s a Python example:
import re
regex = re.compile(r"xy+")
![](https://csdnimg.cn/release/download_crawler_static/10683187/bg14.jpg)
10
Unless dealing with a specific example in a specific language, I will
stick to the /re/ notation in this book.
Literal Characters andMeta Characters
A regex literal character matches itself. The expression /a/ matches any
string that contains the a character.
For instance, the expression /a/ matches the string "andrea" because
both the first and last letters of the string are a. Matching often comes with
simplifications in execution. During execution, the regex engine locates only
the first character of the string, which is an a. Given the character matches
the expression, you are done; there is no need to read the rest of the string.
The expression /a/ does not match the string "ABCDEA" because after
reading the whole string, we did not find a single a character.
All lowercase and uppercase alphabetical characters and all numerical
characters are available as literal characters. Depending on the regex
dialect, some other characters can also be used to match themselves. For
instance, !, %, =, and _ are available in most dialects as literal characters.
Characters such as ., *, ^, $, [, and ] are not literal characters in almost
any dialect. They are meta characters. A meta character is responsible for
denoting an operation similarly to a keyword in a programming language.
Table2-1 describes meta characters.
Table 2-1. Meta Characters
Meta Character Semantics
. Arbitrary character
* Iteration: match any number of times
^ Two semantics: (1) negation, (2) anchor matching the start of the
string or line
$ Anchor matching the end of the string or line
[] Character sets
CHAPTER 2 REGEX SYNTAX 101
剩余155页未读,继续阅读
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
mermaid852
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- AirKiss技术详解:无线传递信息与智能家居连接
- Hibernate主键生成策略详解
- 操作系统实验:位示图法管理磁盘空闲空间
- JSON详解:数据交换的主流格式
- Win7安装Ubuntu双系统详细指南
- FPGA内部结构与工作原理探索
- 信用评分模型解析:WOE、IV与ROC
- 使用LVS+Keepalived构建高可用负载均衡集群
- 微信小程序驱动餐饮与服装业创新转型:便捷管理与低成本优势
- 机器学习入门指南:从基础到进阶
- 解决Win7 IIS配置错误500.22与0x80070032
- SQL-DFS:优化HDFS小文件存储的解决方案
- Hadoop、Hbase、Spark环境部署与主机配置详解
- Kisso:加密会话Cookie实现的单点登录SSO
- OpenCV读取与拼接多幅图像教程
- QT实战:轻松生成与解析JSON数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](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)