content = 'Hello 123 4567 World_This is a Regex Demo' print(len(content))
#逐个匹配单个字符 result = re.match('^Hello\s\d\d\d\s\d{4}\s\w{10}.*Demo$',content) print(result) #输出匹配的字符串 print(result.group()) #输出匹配字符长度 print(result.span())
泛匹配
1 2 3 4 5 6 7 8 9 10 11
import re
content = 'Hello 123 4567 World_This is a Regex Demo' #用.*代表任意个任何字符 result = re.match('^Hello.*Demo$',content) #输出整个匹配结果 print(result) #输出匹配的字符串 print(result.group()) #输出匹配字符长度 print(result.span())
匹配目标
1 2 3 4 5 6 7 8 9
import re
content = 'Hello 1234567 World_This is a Regex Demo' #用()匹配目标1234567 result = re.match('^Hello\s(\d+)\sWorld.*Demo$',content) print(result) print(result.group(1)) #输出匹配字符长度 print(result.span())
贪婪匹配
1 2 3 4 5 6 7
import re
content = 'Hello 1234567 World_This is a Regex Demo' #用.*模式尝试匹配数字串1234567 result = re.match('^He.*\s(\d+)\sWorld.*Demo$',content) print(result) print(result.group(1))
非贪婪匹配
1 2 3 4 5 6 7
import re
content = 'Hello 1234567 World_This is a Regex Demo' #用.*?模式匹配数字串1234567 result = re.match('^H.*?\s(\d+)\sW.*?Demo$',content) print(result) print(result.group(1))
匹配模式
1 2 3 4 5 6 7 8
import re
content = '''Hello 1234567 World_This is a Regex Demo ''' #用re.S意味着.可以代表换行符在内的符号 print(result.group(1))#输出匹配数字串 result = re.match('He.*?\s(\d+).*?Demo$',content,re.S)
转义
1 2 3 4 5 6
import re
content = 'price is $5.00' #直接用$与.代表原字符 result = re.match('price is $5.00',content) print(result)#输出匹配结果
1 2 3 4 5 6
import re
content = 'price is $5.00' #加\代表转义 result = re.match('price is \$5\.00',content) print(result)#输出整个匹配结果
总结:尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、有换行符就用re.S
re.search
re.search 扫描整个字符串并返回第一个成功的匹配。
1 2 3 4 5 6
import re
content = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings' #用re.match匹配以Hello开头至Demo的这一段字符串 result = re.match('Hello.*?(\d+).*?Demo$',content) print(result)
1 2 3 4 5 6 7
import re
content = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings' #用re.search匹配以Hello开头至Demo的这一段字符串 result = re.search('Hello.*?(\d+).*?Demo', content) print(result)#输出整个匹配结果 print(result.group(1))#输出数字字符串