python reptiles: reptiles science will learn regular expressions

Article Updated: 2020-03-30

First, the syntax

1, non-printing characters

Operators Explanation Examples
\cx Match control characters specified by the x \ cM a match Control-Mor a carriage return. The value of x must be A-Zor a-zone.
Otherwise, c as a literal 'c'character.
\f Match for a website page Equivalent \x0cand\cL
\n Matches a newline Equivalent \x0aand\cJ
\r A carriage return match Equivalent \x0dand\cM
\s Matches any whitespace characters
, including spaces, tabs, page breaks, etc.
Equivalent to [ \f\n\r\t\v]
\S Matches any non-whitespace characters Equivalent to [^ \f\n\r\t\v]
\t A matching tab Equivalent \x09and\cI
\v Matching a vertical tab Equivalent \x0band\cK

2, special characters

Operators Explanation Examples
. Express any single character
[ ] Characters, a single character is given in the range of [abc]It represents a, b, c, [a-z]represents a single character az
[^ ] Non characters, a single character is given to the negative range [^abc]Non represent a single character a, b, c of
* Previous character 0once or unlimited expansion abc* Represents ab, abc, abcc, abccc etc.
+ Previous character 1once or unlimited expansion abc+ It represents abc, abcc, abccc etc.
? Previous character 0once or 1twice extended abc? Represents ab, abc
| A left any expression abc|def He represents abc, def
^ Matches the beginning of string ^abcAbc and represents the beginning of a string
$ End of the string abc$Abc represents the end of a string and
, if set RegExp object Multilineproperty, $ also matches '\n'or'\r'
( ) Packet marking, internal use only |operator (abc)He represents abc, (abc|def)represents abc, def
\d digital Equivalent to[0-9]
\w A word character Equivalent to [A-Za-z0-9]
\b Matches a word boundary, that is, refers to the location and spaces between words. For example, er\bmatch neverin er, but can not match verbthe er.
\B Matching non-word boundary. er\BMatches verbin er, but can not match neverthe er.
\d Matches a digit character. Equivalent to [0-9]
\D Matching a non-numeric characters. Equivalent to [^0-9]
\f Match a feed character. Equivalent \x0cand\cL
\n Matches a newline. Equivalent \x0aand\cJ
\r Matching a carriage return. Equivalent \x0dand\cM
\s Matches any whitespace characters, including spaces, tabs, page breaks, and so on. Equivalent to[\f\n\r\t\v]
\S Matches any non-whitespace characters. Equivalent to[^\f\n\r\t\v]
\t A matching tab. Equivalent \x09and\cI
\ Matching a vertical tab. It is equivalent to \x0band \cK.
\w Matches any word character including underscore. Equivalent to[A-Za-z0-9_]
\W Matches any non-word character. Equivalent to[^A-Za-z0-9_]
\xn Matches n, where n is a hexadecimal escape value. Hexadecimal escape values ​​must be determined by two digits long. For example, '\ x41' match "A". '\ X041' is equivalent to '\ x04' & "1". Regular expressions can be used in ASCII encoding. .
\num Matching num, which numis a positive integer. A reference to the acquired match. For example, (.)\1matching two consecutive identical characters.
\n Identifies an octal escape value or a backward reference. If \nat least prior to nthe sub-expression of an acquisition, then n is a reference back.
Otherwise, if noctal numbers (0-7), then n is an octal escape value.
\nm 标识一个八进制转义值或一个向后引用。如果 \nm 之前至少有 nm 个获得子表达式,则 nm 为向后引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的向后引用。

如果前面的条件都不满足,若 nm 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm
\nml 如果 n 为八进制数字 (0-3),且 ml 均为八进制数字 (0-7),则匹配八进制转义值 nml
\un 匹配 n,其中 n 是一个用四个十六进制数字表示的 Unicode 字符。 例如, \u00A9 匹配版权符号 ©

3、限定符

限定符用来指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。有 *+?{n}{n,}{n,m} 共6种。
* +? 限定符都是贪婪的,因为它们会尽可能多的匹配文字,只有在它们的后面加上一个 `? 就可以实现非贪婪或最小匹配。

操作符 说明 实例
{m} 扩展前一个字符 m ab{2}c 表示 abbc
{m,} 扩展前一个字符至少m ab{2,}c 表示 abbc、abbbc、abbbbc等
{m,n} 扩展前一个字符 mn 次(含 n) ab{1,3}c 表示 abc、abcc

4、各个操作符的优先级

注:相同优先级的从左到右进行运算,不同优先级的运算先高后低。各种操作符的优先级从高到低如下:

操作符 描述
\ 转义符
(), (?:), (?=),[] 圆括号和方括号
*,+,?,{n},{n,}, {n,m} 限定符
^, $, \anymetacharacter 位置和顺序
| “或”操作

5、其他说明

操作符 描述
(pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到。
在VBScript 中使用 SubMatches 集合,
在JScript 中则使用 $0…$9 属性。
要匹配圆括号字符,请使用 ‘(’ 或 ‘)’。
(?:pattern) 匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。
这在使用 “或” 字符 (|) 来组合一个模式的各个部分是很有用。

例如, industr(?:y|ies) 就是一个比industry|industries 更简略的表达式。
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配。
也就是说,该匹配不需要获取供以后使用。

例如,Windows(?=95|98|NT|2000) 能匹配 Windows 2000" 中的 "Windows
但不能匹配 Windows 3.1" 中的 "Windows

预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,
而不是从包含预查的字符之后开始。
(?!pattern) 负向预查,在任何不匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配。
也就是说,该匹配不需要获取供以后使用。例如Windows (?!95|98|NT|2000) 能匹配 Windows 3.1 中的 Windows
但不能匹配 Windows 2000 中的 Windows

预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
x|y 匹配 x 或 y。例如,z|food 能匹配 zfood(z|f)ood 则匹配 zoodfood

二、实战演练

语法 描述
^[A-Za-z]+$ 由 26 个字母组成的字符串
^[A-Za-z0-9]+$ 由 26 个字母和数字组成的字符串
^-?\d+$ 整数形式的字符串
^[0-9]*[19][0-9]*$ 正整数形式的字符串
[1-9]\d{5} 中国境内的邮政编码,6位
[\u4e00-\u9fa5] 匹配中文字符
\d{3}-\d{8}|\d{4|-\d{7} 国内电话号码,010-12345678

三、Re库的使用

1、Re库的基本函数

函数 说明
re.search() 在一个字符串中搜索匹配正则的第一个位置,返回 match 对象
re.match() 从一个字符串的开始位置起匹配正则,返回 match 对象
re.findall() 搜索字符串,以列表形式返回全部能匹配的子串
re.split() 将一个字符串按正则匹配结果进行分割,返回列表类型
re.finditer() 搜索字符串,饭后一个匹配结果的迭代类型,每个迭代元素是 match 对象
re.sub() 在一个字符串中替换所有匹配正则表达式的子串,返回替换后的字符串

2、实战演练使用函数

语法格式 re.search(pattern, string, flags=0)

其中 flags 是正则表达式使用时的控制标记,常用的比如:

常用标记 说明
re.I re.IGNORECASE 忽略正则表达式中的大小写,[A-Z] 能匹配到小写字符
re.M re.MULTILINE 正则表达式中的^操作符能够将给定字符串的每行当做匹配开始
re.S re.DOTALL 正则表达式中的.操作符能够匹配所有重复,默认匹配除换行外的所有字符

四、Enjoy!

注1:推荐两个正则在线练习网站
注2:英文可在线练习:Online regex tester and debugger
注3:正则表达式可视化:https://regexper.com/

发布了75 篇原创文章 · 获赞 8 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_21516633/article/details/104766498