Python中re.findall()用法详解

在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。本文重点给大家介绍python中正则表达式 re.findall 用法

re.findall():函数返回包含所有匹配项的列表。返回string中所有与pattern相匹配的全部字串,返回形式为数组。

Python中re.findall()用法详解_第1张图片

示例代码1:【打印所有的匹配项】

import re
 
s = "Long live the people's Republic of China"
ret = re.findall('h', s)
 
print(ret)

运行结果:

Python中re.findall()用法详解_第2张图片

 示例代码2:【如果未找到匹配项,返回空列表】

import re
 
s = "Long live the people's Republic of China"
ret = re.findall('USA', s)
 
print(ret)

运行结果:

示例代码:

import re
 
s = "https://blog.csdn.net/weixin_44799217"
ret = re.findall(r"^http", s)
print(ret)
 
ret2 = re.findall(r"[t,b,s]", s)  # 匹配括号中的其中一个字符
print(ret2)
 
ret3 = re.findall(r"\d\d\d", s)
print(ret3)
 
ret4 = re.findall(r"\d", s)
print(ret4)
 
ret5 = re.findall(r"[^\d]", s)  # 取非
print(ret5)
 
ret6 = re.findall(r"[^https://]", s)  # 取非
print(ret6)

运行结果:

Python中re.findall()用法详解_第3张图片

获取网站中的title:

import requests
import re
 
url = 'https://pz.wendu.com/'
 
response = requests.get(url)
data = response.text
# print(data)
res = re.findall(r'(.*?)', data)[0]
print(res)

运行效果: 

Python中re.findall()用法详解_第4张图片

到此这篇关于Python中re.findall()用法详解的文章就介绍到这了,更多相关Python re.findall()用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Python中re.findall()用法详解)