s = '一共20行代码运行时间13.59s' pat = r'\d+'# +表示匹配数字(\d表示数字的通用字符)1次或多次 r = re.findall(pat,s) print(r)
['20', '13', '59']
4 ?表示前一个字符匹配0或1次
1 2 3 4 5
s = '一共20行代码运行时间13.59s' pat = r'\d+\.?\d+'# ?表示匹配小数点(\.)0次或1次 r = re.findall(pat,s) print(r) # ['20', '13.59']
['20', '13.59']
5 ^匹配字符串的开头
1 2 3 4 5
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'^[emrt]'# 查找以 r = re.findall(pat,s) print(r) # [],因为字符串的开头是字符`T`,不在emrt匹配范围内,所以返回为空
[]
6 re.I 忽略大小写
1 2 3 4 5
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'^[emrt]'# 查找以 r = re.compile(pat,re.I).search(s) print(r) # <re.Match object; span=(0, 1), match='T'> 表明字符串的开头在匹配列表
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'\s?([a-zA-Z]+)' r = re.findall(pat,s) print(r) #['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'\s+' r = re.split(pat,s) print(r) # ['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'\s?([mt][a-zA-Z]*)'# 查找以 r = re.findall(pat,s) print(r) # ['module', 'matching', 'tions', 'milar', 'to', 'those']
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'^([mt][a-zA-Z]*)\s'# 查找以 r = re.compile(pat,re.I).findall(s) print(r) # ['This']