感谢师哥!一个正则表达式居然可以给我分享的这么详细!分享给你

由于最近需要使用爬虫爬取数据进行测试,所以开始了爬虫的填坑之旅,那么首先就是先系统的学习下关于正则相关的知识啦。所以将下面正则方面的知识点做了个整理。语言环境为Python。主要讲解下Python的Re模块。

下面的语法我就主要列出一部分,剩下的在python官网直接查阅即可: docs.python.org/3/library/r…

进群:943752371可以获取Python各类入门学习资料!

**这是我的微信公众号【Python编程之家】各位大佬用空可以关注下,每天更新Python学习方法,感谢! **

《感谢师哥!一个正则表达式居然可以给我分享的这么详细!分享给你》 111111111111.png

一、基础语法总结

1.1、匹配单个字符

a . d D w W s S […] [^…]

匹配单个字符(.)

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>规则:匹配除换行之外的任意字符
In [24]: re.findall(“f.o”,”foo is not fao”)
Out[24]: [‘foo’, ‘fao’]
复制代码
</pre>

匹配任意(非)数字字符(d D)

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>d [0-9]
D [^0-9]
复制代码
</pre>

匹配任意(非)普通字符(w W)

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>w 普通字符 包括[_0-9A-Za-z] 同时也包括汉字
W 非普通字符
复制代码
</pre>

匹配任意(非)空字符(s S)

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>s 匹配任意空字符 [
]
S 匹配任意非空字符
复制代码
</pre>

匹配字符集合([…])

[A-Z][a-z][0-9][_123a-z]

匹配字符集([^…])

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>规则:字符集取非,除列出的字符之外的任意一个字符
[^abc] –> 除a b c之外任意字符
复制代码
</pre>

1.2、匹配多个字符

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>* 匹配0次或者多次

  • 匹配1次或者多次
    ? 匹配0次或者1次
    {m} 匹配m次
    {m,n} 匹配m次到n次区间内的任意一次
    复制代码
    </pre>

1.3、匹配位置

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>^ 匹配开始位置
$ 匹配结束位置
A 匹配开始位置
Z 匹配结束位置
匹配单词边界位置(一般用于首字母大写的匹配)
B 匹配非单词边界问题
复制代码
</pre>

1.4、转义

在正则表达式中有一类特殊字符需要转移,只需要在特殊字符之间加上 表示转移即可

. * + ? ^ $ [] {} () |

1.5、子组

使用() 可以为正则表达式建立内部分组,子组为正则表达式的一部分,可以看做一个内部整体。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>In [61]: re.search(r”(https|http|ftp)://w+.w+.(com|cn)”,”https://www.baidu.com“).group(0)
Out[61]: ‘https://www.baidu.com’
In [62]: re.search(r”(https|http|ftp)://w+.w+.(com|cn)”,”https://www.baidu.com“).group(1)
Out[62]: ‘https’
复制代码
</pre>

1.6、贪婪模式和非贪婪模式

正则表达式的重复匹配总是尽可能多的向后匹配更多的内容。 贪婪模式包括:* + ? {m,n}

非贪婪模式:尽可能少的匹配内容 贪婪模式转换为非贪婪模式:*? +? ?? {m,n}?

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>In [106]: re.findall(r”ab+?”,”abbbbbbbb”)
Out[106]: [‘ab’]
In [107]: re.findall(r”ab??”,”abbbbbbbb”)
Out[107]: [‘a’]
复制代码
</pre>

二、Re模块

《感谢师哥!一个正则表达式居然可以给我分享的这么详细!分享给你》 感谢师哥!一个正则表达式居然可以给我分享的这么详细!分享给你

私信小编007即可获取数十套PDF从零到项目的以及大量的学习资料!

接下来我所有函数里面的参数解释如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>pattern:正则表达式
string:目标字符串
pos:截取目标字符串起始位置
endpose:截取目标字符串结束位置
flags:功能标志
replaceStr:替换的字符串
max:最多替换几处(默认替换全部)
复制代码
</pre>

有上图我们看出来,接下来我们要将的Python中re模块、regex对象、match对象三者之间是存在一定关系的。

  • 1、re模块的compile方法返回一个regex对象
  • 2、re模块和regex对象的finditer()、fullmatch()、match()、search()等方法返回一个match对象
  • 3、他们分别有自己的属性和方法

2.1、compile

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>regex = re.compile(pattern, flags = 0) # 生成正则表达式对象
复制代码
</pre>

2.2、findall

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.findall(pattern,string,pos,endpose) # 从目标字符串中匹配所有符合条件的内容
复制代码
</pre>

2.3、split

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.split(pattern,string,flags) #根据正则表达式对目标字符串进行分割
In [79]: re.split(r’s+’,”Hello World”)
Out[79]: [‘Hello’, ‘World’]
复制代码
</pre>

2.4、sub

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.sub(pattern,replaceStr,string,max,flags)
In [80]: re.sub(r’s+’,”##”,”hello world”)
Out[80]: ‘hello##world’
复制代码
</pre>

2.5、subn

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.subn(pattern,replaceStr,string,max,flags) #功能同sub,但是返回值返回替换后的字符串和替换了几处
In [80]: re.sub(r’s+’,”##”,”hello world”)
Out[80]: (‘hello##world’,1)
复制代码
</pre>

2.6、finditer

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.finditer(pattern,string) #使用正则表达式匹配目标字符串,返回一个match对象,match对象调用group()之后才能拿到值
In [87]: it = re.finditer(r’d+’,”2014nianshiqiqngduo 08aoyun 512dizhen”)
In [88]: for i in it:
….: print(i)
….:
<_sre.SRE_Match object at 0x7f0639767920>
<_sre.SRE_Match object at 0x7f0639767ac0>
<_sre.SRE_Match object at 0x7f0639767920>
In [93]: it = re.finditer(r’d+’,”2014nianshiqiqngduo 08aoyun 512dizhen”)
In [94]: for i in it:
….: print(i.group())
….:
2014
08
512
复制代码
</pre>

2.7、fullmatch

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>fullmatch(pattern,string,flags) #完全匹配目标字符串,相当于加了^ 和 $
复制代码
</pre>

2.8、match

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.match(pattern,string,flags) #匹配目标字符串开头的位置
复制代码
</pre>

2.9、search

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>re.search(pattern,string,flags) # 正则表达式匹配目标字符串,只匹配第一处
复制代码
</pre>

三、一些练习题

3.1、匹配首字母大写的单词

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re
f = open(‘test.txt’)
pattern= r'[A-Z][a-zA-Z]s

pattern= r'[A-Z]S’

L = []
for i in f:
L += re.findall(pattern,i)
print(L)
复制代码
</pre>

test.txt文档内容如下:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>Hello World -12.6
Nihao 123
How are you -12
1.24
asdk 34%,
占比 1/2
2003 – 2005./%
复制代码
</pre>

3.2、匹配数字(正数、负数、小数、百分数、分数)

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import re
pattern = “-?d+((/?d+)|((.)?d+)|((%)?))”
f = open(‘test.txt’)
l = []
for line in f:
l += re.finditer(pattern,line)
for i in l:
print(i.group())
复制代码</pre>

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/54c29fe6ba0d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞