- 论坛徽章:
- 26
|
本帖最后由 substr函数 于 2015-05-27 18:03 编辑
match 一次
+ findall 一次
+ 写这个模式 一次
import re
def repeat(s, p):
true = re.match(r'^({})+$'.format(p), s)
return re.findall(p, s) if true else None
s = "a111za222za333z"
pat = r'a(\d{1,})z'
print repeat(s, pat)
# ['111', '222', '333']
s2 = "a111za222za333x"
print repeat(s2, pat)
# None
s3 = "a111za222zaz"
print repeat(s3, pat)
# None
s4 = "a111za222za22x2z"
print repeat(s4, pat)
# None
pat2 = r'(a\d{1,}z)'
print repeat(s, pat2)
# ['a111z', 'a222z', 'a333z']
print repeat(s2, pat2)
# None
print repeat(s3, pat2)
# None
print repeat(s4, pat2)
# None
|
|