content = """Xiaoshuaib has 100 bananas""" res = re.search('Xi.*?(\d+)\s.*s',content,re.S) print(res.group(1)) # 100
re.findall 获取哦所有规定的匹配值
1 2 3 4 5 6 7 8 9
import re
content = """Xiaoshuaib has 100 bananas; Xiaoshuaib has 100 bananas; Xiaoshuaib has 100 bananas; Xiaoshuaib has 100 bananas;""" res = re.findall('Xi.*?(\d+)\s.*?s;',content,re.S) print(res)
re.sub 替换对应的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import re
content = """Xiaoshuaib has 100 bananas; Xiaoshuaib has 100 bananas; Xiaoshuaib has 100 bananas; Xiaoshuaib has 100 bananas;""" content = re.sub('\d+','250',content) print(content)
# print # Xiaoshuaib has 250 bananas; # Xiaoshuaib has 250 bananas; # Xiaoshuaib has 250 bananas; # Xiaoshuaib has 250 bananas;
re.complie 匹配封装
1 2 3 4 5 6 7
import re
content = 'Xiaoshuaib has 100 bananas' pattern = re.complie('^Xi.*(\d+)\s.*s$')