content = 'Hello 123 4567 World_This is a Regex Demo'
#常规匹配
result = re.match('^Hello\s\d\d\d\s\d{4}\s\w{10}.*Demo$',content)
print(result)
print(result.group())
print(result.span())
#泛匹配
result_1 = re.match('^Hello.*Demo$',content)
print(result_1.group())
#匹配目标
result_2 = re.match('^Hello\s(\d+\s\d+).*Demo$',content)
print(result_2.group(1))
#贪婪匹配
result_3 = re.match('^He.*(\d+\s\d+).*Demo$',content)
print(result_3.group(1))
#非贪婪模式
result_4 = re.match('^He.*?(\d+\s\d+).*Demo$',content)
print(result_4.group(1))
#匹配模式
content_1 = '''Hello 123 4567 World_This
is a Regex Demo'''
result_5 = re.match('^He.*?(\d+\s\d+).*Demo$',content_1,re.S)
print(result_5)
转义(略)
re.search()扫描整个字符串,能用serch不用match,但也只是一个
re.findall()搜索整个字符串,以列表形式返回所能匹配字符串
re.sub()替换字符串每一个匹配的字符串后返回替换后的字符串,\1代表字符串本身
pattern = re.compile()
网友评论