findall

2019-05-18  本文已影响0人  G_uest
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'>
上一篇 下一篇

猜你喜欢

热点阅读