推荐测试网站 :https://goregex.cn/

匹配替换

a = re.sub(r'\d+', 'hello', 'my numer is 400 and door num is 200')
print(a)
'my numer is hello and door num is hello' #数字400 200 被hello替换

match

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0) # The entire match
'Isaac Newton'
>>> m.group(1) # The first parenthesized subgroup.
'Isaac'
>>> m.group(2) # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2) # Multiple arguments give us a tuple.
('Isaac', 'Newton')

findall

.. 表示任意一个字符
pattern = re.compile(r"a..")
s = pattern.findall("abc1agf2afr3art4")
for i in s:
print(i)

search

全部进行查询