findall

作者: G_uest | 来源:发表于2019-05-18 13:52 被阅读0次
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'>

相关文章

网友评论

      本文标题:findall

      本文链接:https://www.haomeiwen.com/subject/ogctzqtx.html