What is a regular expression?
A regular expression is a group of letters and symbols special text, which can be used to find sentences that meet the format you want from the text.
A regular expression is when matching strings from left to right in a body string a style of. The term "Regular expression" is a bit tricky to pronounce, and we often use the abbreviated terms "regex" or "regexp". Regular expressions can replace strings in text according to certain matching patterns from a basic string, validate forms, extract strings, etc.
Imagine you are writing an application, and then you want to set an User naming rules allow user names to contain characters, numbers, underscores and hyphens, and limit the number of characters so that the name does not look so ugly. We use the following regular expression to verify a username:
The above regular expression can accept john_doe
, jo-hn_doe
, john12_as
.
But it does not match Jo
, because it contains uppercase letters and is too short.
Directory
2.5 (...) Characteristic group
2.7 Convert special characters
# 3. Abbreviated character set
4. Zero-width assertion (before and after preview)
4.1 ?=...Positive lookahead assertion
4.2 ?!... Negative lookahead assertion
5.2 Global search(Global search)
5.3 Multiline modifier (Multiline)
Regular expression actually This is the format used when performing a search, and is a combination of letters and numbers. For example: a regular expression the, which represents a rule: starting with the letters
tMetacharacters | Description |
---|---|
. | Period matches any single character except newline. |
[ ] | Character type. Matches any character within square brackets. |
[^ ] | Negative character type. Matches any characters except square brackets |
* | ## Matches >= 0 repeated characters before the * sign.|
Match >= 1 repeated character before the number. | |
Mark? The preceding characters are optional. | |
Matches num characters before the braces (n | |
Character set, matching strings that are exactly equal to xyz. | |
or operator, matches the characters before or after the symbol. | |
escape character, used to match some reserved characters | [ ] ( ) { } . * ? ^ $ \ |
|
Match from the beginning line. | |
Match from the end. |
简写 | 描述 |
---|---|
. | 除换行符外的所有字符 |
\w | 匹配所有字母数字, 等同于 [a-zA-Z0-9_]
|
\W | 匹配所有非字母数字, 即符号, 等同于: [^\w]
|
\d | 匹配数字: [0-9]
|
\D | 匹配非数字: [^\d]
|
\s | 匹配所有空格字符, 等同于: [\t\n\f\r\p{Z}]
|
\S | 匹配所有非空格字符: [^\s]
|
\f | 匹配一个换页符 |
\n | 匹配一个换行符 |
\r | 匹配一个回车符 |
\t | 匹配一个制表符 |
\v | 匹配一个垂直制表符 |
\p | 匹配 CR/LF (等同于 \r\n ),用来匹配 DOS 行终止符 |
先行断言和后发断言都属于非捕获簇(不捕获文本 ,也不针对组合计进行计数). 先行断言用于判断所匹配的格式是否在另一个确定的格式之前, 匹配结果不包含该确定格式(仅作为约束).
例如, 我们想要获得所有跟在 $
符号后的数字, 我们可以使用正后发断言 (?.
这个表达式匹配 <code>$
开头, 之后跟着 0,1,2,3,4,5,6,7,8,9,.
这些字符可以出现大于等于 0 次.
零宽度断言如下:
符号 | 描述 |
---|---|
?= | 正先行断言-存在 |
?! | 负先行断言-排除 |
? | 正后发断言-存在 |
? | 负后发断言-排除 |
?=...
正先行断言 ?=...
正先行断言, 表示第一部分表达式之后必须跟着 ?=...
定义的表达式.
返回结果只包含满足匹配条件的第一部分表达式.
定义一个正先行断言要使用 ()
. 在括号内部使用一个问号和等号: (?=...)
.
正先行断言的内容写在括号中的等号后面.
例如, 表达式 (T|t)he(?=\sfat)
匹配 The
和 the
, 在括号中我们又定义了正先行断言 (?=\sfat)
,即 The
和 the
后面紧跟着 (空格)fat
.
"(T|t)he(?=\sfat)" => The fat cat sat on the mat.
?!...
负先行断言 负先行断言 ?!
用于筛选所有匹配结果, 筛选条件为 其后不跟随着断言中定义的格式.正先行断言
定义和 负先行断言
一样, 区别就是 =
替换成 !
也就是 (?!...)
.
表达式 (T|t)he(?!\sfat)
匹配 The
和 the
, 且其后不跟着 (空格)fat
.
"(T|t)he(?!\sfat)" => The fat cat sat on the mat.
? 正后发断言
正后发断言 记作(? 用于筛选所有匹配结果, 筛选条件为 其前跟随着断言中定义的格式.
例如, 表达式 <code>(? 匹配 <code>fat
和 mat
, 且其前跟着 The
或 the
.
"(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.
? 负后发断言
负后发断言 记作 (? 用于筛选所有匹配结果, 筛选条件为 其前不跟随着断言中定义的格式.
例如, 表达式 <code>(? 匹配 <code>cat
, 且其前不跟着 The
或 the
.
"(?<!(T|t)he\s)(cat)" => The cat sat on cat.
Flags are also called pattern modifiers, because they can be used to modify the search results of expressions. These flags can be used in any combination and are part of the entire regular expression.
Flags | Description |
---|---|
i | Ignore case. |
g | Global search. |
m | Multiple lines: Anchor metacharacter^ $ The working range is at the beginning of each line. |
修饰语 i
用于忽略大小写.
例如, 表达式 /The/gi
表示在全局搜索 The
, 在后面的 i
将其条件修改为忽略大小写, 则变成搜索 the
和 The
, g
表示全局搜索.
"the" => The fat cat sat on the mat.
"/The/gi" => The fat cat sat on the mat.
修饰符 g
常用于执行一个全局搜索匹配, 即(不仅仅返回第一个匹配的, 而是返回全部).
例如, 表达式 /.(at)/g
表示搜索 任意字符(除了换行) + at
, 并返回全部结果.
"/.(at)/" => The fat cat sat on the mat.
"/.(at)/g" => The fat cat sat on the mat.
多行修饰符 m
常用于执行一个多行匹配.
像之前介绍的 (^,$)
用于检查格式是否是在待检测字符串的开头或结尾. 但我们如果想要它在每行的开头和结尾生效, 我们需要用到多行修饰符 m
.
例如, 表达式 /at(.)?$/gm
表示小写字符 a
后跟小写字符 t
, 末尾可选除换行符外任意字符. 根据 m
修饰符, 现在表达式匹配每行的结尾.
"/.at(.)?$/" => The fat cat sat on the mat.
"/.at(.)?$/gm" => The fat cat sat on the mat.
正则表达式默认采用贪婪匹配模式,在该模式下意味着会匹配尽可能长的子串。我们可以使用 ?
将贪婪匹配模式转化为惰性匹配模式。
"/(.*at)/" => The fat cat sat on the mat.
"/(.*?at)/" => The fat cat sat on the mat.
相关推荐:
2. Regular expression video tutorial