findall 返回一个list列表,但当正则有多个分组时,list列表中的元素为tuple元组
代码:
import re
str = "df223232akjhgkjad2145s4dfsfd545454sdaf45asdf5545af253gf55dsf"
pattern_tuple = re.compile('([a-z]*)f([\d]*)')
data_tuple = pattern_tuple.findall(str)
print(data_tuple)
print(type(data_tuple))
pattern = re.compile('[a-z]*f')
data = pattern.findall(str)
print(data)
print(type(data))
输出结果:
[('d', '223232'), ('dfs', ''), ('sda', '45'), ('asd', '5545'), ('a', '253'), ('g', '55'), ('ds', '')]
<class 'list'>
['df', 'dfsf', 'sdaf', 'asdf', 'af', 'gf', 'dsf']
<class 'list'>
网友评论