s = '一共20行代码运行时间13.59s' pat = r'\d+'# +表示匹配数字(\d表示数字的通用字符)1次或多次 r = re.findall(pat,s) print(r) # ['20', '13', '59']
? 表示前一个字符匹配0或1次
1 2 3 4 5 6 7
import re
s = '一共20行代码运行时间13.59s' pat = r'\d+\.?\d+'# ?表示匹配小数点(\.)0次或1次 r = re.findall(pat,s) print(r) # ['20', '13.59']
^ 匹配字符串的开头
1 2 3 4 5 6 7
import re
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匹配范围内,所以返回为空
re.I 忽略大小写
1 2 3 4 5 6 7
import re
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) #['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?([a-zA-Z]+)' r = re.findall(pat,s) print(r) #['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
使用 split() 直接分割单词
使用以上方法分割单词,不是简洁的,仅仅为了演示。分割单词最简单还是使用 split() 。
1 2 3 4 5 6
import re
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']
提取以m或t开头的单词,忽略大小写
下面出现的结果不是我们想要的,原因出在 ? 上!
1 2 3 4 5 6
import re
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']
先分割,再查找满足要求的单词
使用 match 表示是否匹配
1 2 3 4 5 6 7
import re
s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'\s+' r = re.split(pat,s) res = [i for i in r if re.match(r'[mMtT]',i)] print(res) # ['This', 'module', 'matching', 'to', 'those']