python中正则表达式 re.findall 用法

在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。

其中,re.findall() 函数可以遍历匹配,可以获取字符串中所有匹配的字符串,返回一个列表。

在python源代码中,展示如下: 搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。找到 RE 匹配的所有子串,并把它们作为一个迭代器返回。

def findall(pattern, string, flags=0):

第一个参数,正则表达式

第二个参数,搜索的是那些字符串

第三个参数,匹配的模式,其中re.S使匹配包括换行在内的所有字符。findall()函数是逐行匹配的。

返回string中所有与pattern相匹配的全部字串,返回形式为数组

如果想要使用re.findall函数,必须引用re包

import re
import re
regular_v1 = re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v1)

返回结果为: ['docs'] 是个数组的形式

用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串

import re
regular_v3 = re.findall(r"html$","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v3)

返回结果为: ['html']是个数组的形式