正则表达式:regular expression regex RE
正则表达式是用来简洁表达一组字符串的表达式
通用的字符串表达框架
简洁表达一组字符串的表达式
针对字符串表达“简洁”和“特征”思想的工具
判断某字符串的特征归属
正则表达式在文本处理中十分常用
表达文本类型的特征(病毒、入侵等)
同时查找或替换一组字符串
匹配字符串的全部或部分
正则表达式的使用
编译:将符合正则表达式语法的字符串转换成正则式表达特征
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519152540715-1679569543.png)
正则表达式的语法
正则表达式语法由字符和操作符构成
正则表达式的常用操作符
正则表达式的常用操作符
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519154547434-1959541417.png)
实例
IP地址
Re库的基本使用
Re库介绍:Re库是Python的标准库,主要用于字符串匹配
正则表达式的表示类型
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519160756382-370792241.png)
需要多加一个来表示正则表达式的
因此当正则表达式含有转义字符时,需要用raw string
re库的主要函数
大部分都返回match对象
re.search(pattern, string,flags=0)
pattern:正则表达式的字符串或原生字符串表示
string:待匹配字符串
flags:正则表达式使用时的控制标记
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519162859635-827393441.png)
re.s可以匹配所有字符
match = re.search(r'[1-9]d{5}','WFR 100860')
if match:
print(match.group(0))
else:
print('error')
100860
re.match(pattern, string,flags=0) 各参数与前述相同
match = re.match(r'[1-9]d{5}','WFR 100860')#此时的match 为空
if match:
print(match.group(0))
else:
print('error')
error
match = re.match(r'[1-9]d{5}','123456 DF')
if match:
print(match.group(0))
else:
print('error')
12re.findall(pattern, string, flags=0) 各参数与前述相同
match = re.findall(r'[1-9]d{5}','123456 DF 102523')
print(match)
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519170543614-2022759739.png)
s = re.findall(r'[d]','123.3 2.3 1000')
print(s)
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519224533802-1318024418.png)
s = re.findall(r'[d.]','123.3 2.3 1000')
print(s)
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519224613260-1179262457.png)
s = re.findall(r'[d.]*','123.3 2.3 1000')
print(s)
re.split
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519170746332-1375338215.png)
match = re.split(r'[1-9]d{5}','SFF123456 DF 102523')
print(match) #删除了匹配部分
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519171050692-503283791.png)
match = re.split(r'[1-9]d{5}','SFF123456 DF 102523',maxsplit=1)
print(match)
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519171155055-1220566546.png)
re.finditer(pattern, string, flags=0) 各参数与前述相同
for i in re.finditer(r'[1-9]d{5}','SFF123456 DF 102523'):
if i :#一定要先判断,如果为空会报错NoneType
print(i.group(0))
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519171550467-1880024968.png)
re.sub
print(re.sub(r'[1-9]d{5}',':okok','SFF123456 DF 102523'))
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519171949122-1021900515.png)
print(re.sub(r'[1-9]d{5}',':okok','SFF123456 DF 102523',count=1))
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519172024984-760338909.png)
re库的另一种等价写法
rst = pat.search('BIT 100081') #此时需要把正则表达式参数去掉
regex = re.compile(pattern,flags=0)
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519173257215-868916506.png)
也就等价于
大部分都返回match对象
Re库的match对象
match对象的属性
match = re.search(r'[1-9]d{5}','NJI 102346')
if match:
print(match.group(0))
print(type(match))
print(match.string)
print(match.re)
print(match.pos)
print(match.endpos)
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519175109116-732178545.png)
match = re.search(r'[1-9]d{5}','NJI 102346')
if match:
print(match.group(0))
print(match.start())
print(match.end())
print(match.span())
![](https://img2020.cnblogs.com/blog/1437100/202005/1437100-20200519175614147-2028966766.png)
Re库的贪婪匹配和最小匹配
.*
.*?
re库总结