This article mainly introduces how to use regular expressions to extract data in python. It has very good reference value and I hope it will be helpful to everyone. If there are mistakes or things that have not been fully considered, please let me know.
A regular expression is a special sequence of characters that helps you easily check whether a string matches a certain pattern.
The re module brings full regular expression functionality to the Python language.
The compile function creates a regular expression object consisting of a pattern string and optional flags parameters. This object has a set of methods for regular expression matching and replacement.
The re module also provides functions that do exactly what these methods do, taking a pattern string as their first argument.
model | describe |
---|---|
^ | matches the beginning of the string |
$ | Matches the end of a string. |
. | Matches any character, except newline, and when the re.DOTALL flag is specified, matches any character including newline. |
[...] | Used to represent a group of characters, listed separately: [amk] matches 'a', 'm' or 'k' |
[^...] | Characters not in []: [^abc] matches characters other than a, b, c. |
re* | * matches 0 or more times. Greedy way, re stands for regular expression |
re+ | + Match 1 or more times. |
re? | ?Match 0 or 1 times, non-greedy way, match 0 times means that the expression is followed by empty also match |
re{ n} | Matches n consecutive expressions. For example, o{2}, which matches two consecutive o's, cannot match the "o" in "Bob", but can match two o's in "food". |
re{ n,} | Matches n occurrences of the preceding expression. For example, o{2,} would not match the "o" in "Bob", but would match all o's in "foooood". "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*". |
re{ n, m} | Indicates to match the consecutive previous expressions at least n times and at most m times. The expression oil{3,4} means matching consecutive oil characters at least 3 times and at most 4 times |
a| b | match a or b |
(re) | Group regular expressions and remember matched text |
Examples of common regular expressions
character match
example | describe |
---|---|
python | matches "python". |
character class
example | describe |
---|---|
[Pp]ython | matches "Python" or "python" |
rub[ye] | matches "ruby" or "rube" |
[credit] | matches any letter in brackets |
[0-9] | matches any number. Similar to [0123456789] |
[a-z] | matches any lowercase letter |
[A-Z] | matches any uppercase letter |
[a-zA-Z0-9] | matches any letter and number |
[^aiyou] | All characters except aeiou letters |
[^0-9] | matches any character except digits |
special character class
example | describe |
---|---|
. | Matches any single character except "\n". To match any character including '\n', use a pattern like '[.\n]'. |
\d | Matches a numeric character. Equivalent to [0-9]. |
\D | Matches a non-numeric character. Equivalent to [^0-9]. |
\s | Matches any whitespace character, including spaces, tabs, form feeds, and so on. Equivalent to [ \f\n\r\t\v]. |
\S | Matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v]. |
\w | Matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'. |
\W | Matches any non-word character. Equivalent to '[^A-Za-z0-9_]'. |
re.match only matches the beginning of the string, if the beginning of the string does not match the regular expression, the match fails and the function returns None; while re.search matches the entire string until the first match is found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
re.compile 函数
compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用。
语法格式为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
执行结果:
None
None
<re.Match object; span=(3, 5), match='12'>
12
3
5
(3, 5)
括号()-分组
括号称之为 正则表达式的 组选择。
组 就是把 正则表达式 匹配的内容 里面 其中的某些部分 标记为某个组。
我们可以在 正则表达式中 标记 多个 组
为什么要有组的概念呢?因为我们往往需要提取已经匹配的 内容里面的 某些部分的信息。
前面,我们有个例子,从下面的文本中,选择每行逗号前面的字符串,也 包括逗号本身 。
苹果,苹果是绿色的
橙子,橙子是橙色的
香蕉,香蕉是黄色的
就可以这样写正则表达式 ^.*,
。
但是,如果我们要求 不要包括逗号 呢?
当然不能直接 这样写 ^.*
因为最后的逗号 是 特征 所在, 如果去掉它,就没法找 逗号前面的了。
但是把逗号放在正则表达式中,又会包含逗号。
解决问题的方法就是使用 组选择符 : 括号。
我们这样写 ^(.*),
,结果如下
大家可以发现,我们把要从整个表达式中提取的部分放在括号中,这样 水果 的名字 就被单独的放在 组 group 中了。
对应的Python代码如下
1 2 3 4 5 6 7 8 |
|
多个分组时,怎么取每个分组的值。
比如,我们要从下面的文本中,提取出每个人的 名字 和对应的 手机号
张三,手机号码15945678901
李四,手机号码13945677701
王二,手机号码13845666901
可以使用这样的正则表达式 ^(.+),.+(\d{11})
可以写出如下的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
当有多个分组的时候,我们可以使用 (?P<分组名>...)
这样的格式,给每个分组命名。
这样做的好处是,更方便后续的代码提取每个分组里面的内容
比如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
总结:正则若匹配成功,match()/search()返回的是Match对象,finditer()返回的是Match对象的迭代器,获取匹配结果需要调用Match对象的group()、groups或group(index)方法。
group()
:母串中与模式pattern匹配的子串;group(0)
:结果与group()一样;groups()
:所有group组成的一个元组,group(1)是字符串中第一个匹配成功的子串分组,group(2)是第二个,依次类推,如果index超了边界,抛出IndexError;findall()
:返回的就是所有匹配的子串数组,就是子串元组组成的列表,例如上面的例子,母串中的第一行组成一个元组,第二行组成一个元组,这些元组共同构成一个list,就是findall()的返回结果。
方括号-匹配几个字符之一
方括号表示要匹配 指定的几个字符之一 。
比如
[abc]
可以匹配 a, b, 或者 c 里面的任意一个字符。等价于 [a-c]
。
[a-c]
中间的 - 表示一个范围从a 到 c。
如果你想匹配所有的小写字母,可以使用 [a-z]
一些 元字符 在 方括号内 失去了魔法, 变得和普通字符一样了。
比如
[akm.]
匹配 a k m .
里面任意一个字符
这里 .
在括号里面不在表示 匹配任意字符了,而就是表示匹配 .
这个 字符
如果在方括号中使用 ^
, 表示 非
方括号里面的字符集合。
比如
1 2 3 4 5 6 |
|
[^\d]
表示,选择非数字的字符
输出结果为:
a
b
c
d
e
切割字符串
字符串 对象的 split
方法只适用于 简单的字符串分割。 有时,你需要更加灵活的字符串切割。
比如,我们需要从下面字符串中提取武将的名字。
1 |
|
我们发现这些名字之间, 有的是分号隔开,有的是逗号隔开,有的是空格隔开, 而且分割符号周围还有不定数量的空格
这时,可以使用正则表达式里面的 split 方法:
1 2 3 4 5 6 |
|
正则表达式 [;,\s]\s*
指定了,分割符为 分号、逗号、空格 里面的任意一种均可,并且 该符号周围可以有不定数量的空格。
字符串替换
匹配模式替换
字符串 对象的 replace
方法只适应于 简单的 替换。 有时,你需要更加灵活的字符串替换。
比如,我们需要在下面这段文本中 所有的 链接中 找到所以 /avxxxxxx/
这种 以 /av
开头,后面接一串数字, 这种模式的字符串。
然后,这些字符串全部 替换为 /cn345677/
。
1 2 3 4 5 6 7 8 9 |
|
被替换的内容不是固定的,所以没法用 字符串的replace方法。
这时,可以使用正则表达式里面的 sub 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
sub 方法就是也是替换 字符串, 但是被替换的内容 用 正则表达式来表示 符合特征的所有字符串。
比如,这里就是第一个参数 /av\d+?/
这个正则表达式,表示以 /av
开头,后面是一串数字,再以 /
结尾的 这种特征的字符串 ,是需要被替换的。
第二个参数,这里 是 '/cn345677/'
这个字符串,表示用什么来替换。
第三个参数是 源字符串。
指定替换函数
刚才的例子中,我们用来替换的是一个固定的字符串 /cn345677/
。
如果,我们要求,替换后的内容 的是原来的数字+6, 比如 /av66771949/
替换为 /av66771955/
。
怎么办?
这种更加复杂的替换,我们可以把 sub的第2个参数 指定为一个函数 ,该函数的返回值,就是用来替换的字符串。
如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
总结
以上为个人经验,希望能给大家一个参考。
点击拿去
50G+学习视频教程
100+Python初阶、中阶、高阶电子书籍