7.9 通配字符
在正则表达式中,.(句点)字符称为“通配符”
1 >>> atRegex = re.compile(r‘.at‘) 2 >>> atRegex.findall(‘The cat in the hat sat on the flat mat.‘) 3 [‘cat‘, ‘hat‘, ‘sat‘, ‘lat‘, ‘mat‘]
7.9.1 用点-星匹配所有字符
可以用点-星(.*)表示“任 意文本”
1 >>> nameRegex = re.compile(r‘First Name: (.*) Last Name: (.*)‘) 2 >>> mo = nameRegex.search(‘First Name: Al Last Name: Sweigart‘) 3 >>> mo.group(1) 4 ‘Al‘ 5 >>> mo.group(2) 6 ‘Sweigart‘
点-星使用“贪心”模式:它总是匹配尽可能多的文本。要用“非贪心”模式匹配 所有文本,就使用点-星和问号
1 >>> nongreedyRegex = re.compile(r‘<.*?>‘) 2 >>> mo = nongreedyRegex.search(‘<To serve man> for dinner.>‘) 3 >>> mo.group() 4 ‘<To serve man>‘ 5 >>> greedyRegex = re.compile(r‘<.*>‘) 6 >>> mo = greedyRegex.search(‘<To serve man> for dinner.>‘) 7 >>> mo.group() 8 ‘<To serve man> for dinner.>‘
两个正则表达式都可以翻译成“匹配一个左尖括号,接下来是任意字符,接下 来是一个右尖括号”。但是字符串‘ for dinner.>‘对右肩括号有两种可能的 匹配。在非贪心的正则表达式中,Python 匹配最短可能的字符串:‘‘。 在贪心版本中,Python 匹配最长可能的字符串:‘ for dinner.>‘。
7.9.2 用句点字符匹配换行
1 >>> noNewlineRegex = re.compile(‘.*‘) 2 >>> noNewlineRegex.search(‘Serve the public trust.\nProtect the innocent. 3 \nUphold the law.‘).group() 4 ‘Serve the public trust.‘ 5 >>> newlineRegex = re.compile(‘.*‘, re.DOTALL) 6 >>> newlineRegex.search(‘Serve the public trust.\nProtect the innocent.
\nUphold the law.‘).group()
‘Serve the public trust.\nProtect the innocent.\nUphold the law.‘
7.11 不区分大小写的匹配
1 >>> regex1 = re.compile(‘RoboCop‘) 2 >>> regex2 = re.compile(‘ROBOCOP‘) 3 >>> regex3 = re.compile(‘robOcop‘) 4 >>> regex4 = re.compile(‘RobocOp‘) 5 >>> robocop = re.compile(r‘robocop‘, re.I) 6 >>> robocop.search(‘RoboCop is part man, part machine, all cop.‘).group() 7 ‘RoboCop‘ 8 >>> robocop.search(‘ROBOCOP protects the innocent.‘).group() 9 ‘ROBOCOP‘ 10 >>> robocop.search(‘Al, why does your programming book talk about robocop so much?‘).group() 11 ‘robocop‘