1.python中使用正则表达式的一般步骤
>>> import re >>> phoneNumber=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') >>> mo=phoneNumber.search('My number is 415-555-4242') >>> mo.group()
输出结果:415-555-4242
2.利用括号分组
>>> import re >>> phoneNumRegex=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') >>> mo=phoneNumRegex.search('My number is 415-555-5252.') >>> mo.group(1) '415' >>> mo.group(2) '555-5252' >>> mo.group(0) '415-555-5252' >>> mo.group() '415-555-5252' >>> mo.groups() ('415', '555-5252')
正则表达式字符串中的第一对括号是第1组,第二组括号是第2组。向group()方法传入0或不传入参数,将返回整个匹配的文本。groups()返回多个值得元祖。
3.如果正则表达式匹配括号就要加转义字符‘\’
>>> import re >>> phoneNumRegex=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') >>> phoneNumRegex=re.compile(r'(\(\d\d\d\))-(\d\d\d-\d\d\d\d)') >>> mo=phoneNumRegex.search('My number is (415)-555-5252.') >>> mo.group(1) '(415)' >>> mo.group(2) '555-5252'
4用管道匹配多个分组
>>> import re >>> batRegex=re.compile(r'Bat(man|mobile|copter|bat)') >>> mo=batRegex.search('Batmobile lost a wheel,the Batman found it') >>> mo.group() 'Batmobile' >>> mo.group(1) 'mobile'
方法调用mo.group()返回了完全匹配的文本‘Batmobile’,而mo.group(1)只是返回第一个括号分组内匹配的文本'mobile'
5贪心与非贪心匹配
python的正则表达式默认的是 贪心 的,这表示在有二义的情况下,它们会尽可能匹配最长的字符串。花括号的“非贪心”版本匹配尽可能最短的字符串,即在结束的花括号后跟着一个问号
>>> import re
>>> HaRegex=re.compile(r'(ha){3,5}?')
>>> mo=HaRegex.search('hahahahahaha')
>>> mo.group()
'hahaha'
6.findall()方法
search()返回的是一个Match对象,包含被查找字符串中的‘第一次’匹配的文本
findall()方法将返回一组字符串,包含被查找字符串中的所有匹配;如果在正则表达式中有分组,那么findall将返回元组的列表。每个元组表示一个找到的匹配,其中的项就是正则表达式中的每个分组的匹配字符串
>>> phoneNumRegex=re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') >>> phoneNumRegex.findall('Cell:415-555-9999 work:212-555-0000.') [('415', '555', '9999'), ('212', '555', '0000')]
7.用 句点字符匹配换行
句点字符匹配除行外的所有字符。通过传入re.DOTALL作为re.compile()的第二个参数,可以让句点字符匹配所有字符,包括换行字符
>>> noNewlineRegex=re.compile('.*') >>> noNewlineRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group() 'Serve the public trust.' ------------------------------------------------------- >>> newlineRegex=re.compile('.*',re.DOTALL) >>> newlineRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group() 'Serve the public trust.\nProtect the innocent.\nUphold the law.'
8.不区分大小写的匹配
要让正则表达式不区分大小写,可以向re.compile()传入re.IGNORECASE或re.I,作为第二个参数
>>> robocop=re.compile(r'robocop',re.I) >>> robocop.search('RoboCop is part man,part machine,all cop.').group() 'RoboCop'
9.用sub()方法替代字符串
>>> namesRegex=re.compile(r'Agent \w+') >>> namesRegex.sub('CENSORED','Agent Alice gave the secret documents to Agent Bob.') 'CENSORED gave the secret documents to CENSORED.' -------------------------------------------------------------------------------- >>> agentNameRegex=re.compile(r'Agent (\w)\w*') >>> agentNameRegex.sub(r'\1****','Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.') 'A**** told C**** that E**** knew B**** was a double agent.'
10.可以向re.compile()传入变量re.VERBOSE,作为第二个参数,忽略正则表达式字符串中的空白符和注释。
phoneRegex=re.compile(r'''( (\d{3}|\(\d{3}\))? (\s|-|\.)? \d{3} (\s|-|\.) \d{4} (\s*(ext|x|ext.)\s*\d{2,5})? )''',re.VERBOSE)