Python学习3-7.1-7.3正则表达式查找文本
本文为学习python编程时所记录的笔记,仅供学习交流使用。
7.1 不使用正则表达式查找文本
def isPhoneNumber(text):
if len(text)!=12:
return False
for i in range(0,3):
if not text[i].isdecimal():
return False
if text[3]!='-':
return False
for i in range(4,7):
if not text[i].isdecimal():
return False
if text[7]!='-':
return False
for i in range(8,12):
if not text[i].isdecimal():
return False
return True
'''
print('415-555-4242 is a phone number:')
print(isPhoneNumber('415-555-4242'))
print('Moshi moshi is a phone number:')
print(isPhoneNumber('Moshi moshi'))
'''
message='Call me at 415-555-1011 tomorrow.415-555-9999 is my office.'
for i in range(len(message)):
chunk=message[i:i+12]
if isPhoneNumber(chunk):
print('Phone number found:'+chunk)
print('Done')
7.2 使用正则表达式查找文本
正则表达式,简称regex,是文本模式的描述方法
\d 表示一位数字字符,即0-9的数字
\d\d\d-\d\d\d-\d\d\d\d
在一个模式后加上花括号{3}表示匹配这个模式3次
\d{3}-\d{3}-\d{4}
>>> import re
>>> phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
>>> mo=phoneNumRegex.search('My number is 415-555-4242.')
>>> print('Phone number found:'+mo.group())
Phone number found:415-555-4242
1、因正则表达式的函数在re模块中,因此导入re模块。
2、向re.compile传入一个字符串值,表示正则表达式,它将返回一个Regex对象
3、Regex对象的search方法查找传入的字符串,如果没找到,search方法返回None,如果找到了search方法将返回一个match对象
4、match对象有一个group方法,返回被查找字符串中实际匹配的文本
7.3 用正则表达式匹配更多模式
7.3.1 利用括号分组
>>> phoneNumRegex=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
>>> mo=phoneNumRegex.search('My number is 415-555-4242.')
>>> mo.group(1)
'415'
>>> mo.group(2)
'555-4242'
>>> mo.group(0)
'415-555-4242'
>>> mo.group()
'415-555-4242'
>>> mo.groups()
('415', '555-4242')
>>> areaCode,mainNumber=mo.groups()
>>> print(areaCode)
415
>>> print(mainNumber)
555-4242
当需要匹配括号时,用\转义
>>> phoneNumRegex=re.compile(r'(\(\d\d\d\))(\d\d\d-\d\d\d\d)')
>>> mo=phoneNumRegex.search('My phone number is (415)555-4242.')
>>> mo.group(1)
'(415)'
>>> mo.group(2)
'555-4242'
7.3.2 用管道匹配多个分组
字符“|”称为管道,希望匹配多个表达式中的一个时,可以使用
>>> heroRegex=re.compile(r'Batman|Tina Fey')
>>> mo1=heroRegex.search('Batman and Tina Fey.')
>>> mo1.group()
'Batman'
>>> mo2=heroRegex.search('Tina Fey and Batman.')
>>> mo2.group()
'Tina Fey'
括号和管道匹配一起用
>>> batRegex=re.compile(r'Bat(man|mobile|copter|bat)')
>>> mo=batRegex.search('Batmobile lost a wheel.')
>>> mo.group()
'Batmobile'
>>> mo.group(1)
'mobile'
7.3.3 用问号实现可选匹配
>>> batRegex=re.compile(r'Bat(wo)?man')
>>> mo1=batRegex.search('The adventure of Batman')
>>> mo1.group()
'Batman'
>>> mo2=batRegex.search('The adventrue of Batwoman.')
>>> mo2.group()
'Batwoman'
?匹配问号之前的分组0次或1次
>>> phoneRegex=re.compile(r'(\d\d\d-)?\d\d\d-\d\d\d\d')
>>> mo1=phoneRegex.search('My number is 415-555-4242.')
>>> mo1.group()
'415-555-4242'
>>> mo2=phoneRegex.search('My number is 555-4242')
>>> mo2.group()
'555-4242'
7.3.4 用星号匹配零次或多次
>>> batRegex=re.compile(r'Bat(wo)*man')
>>> mo1=batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'
>>> mo2=batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'
>>> mo3=batRegex.search('The Adventures of Batwowowoman')
>>> mo3.group()
'Batwowowoman'
>>> mo4=batRegex.search('The Adventures of Batxxman')
>>> mo4.group()
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
mo4.group()
AttributeError: 'NoneType' object has no attribute 'group'
7.3.5 用加号匹配一次或多次
>>> batRegex=re.compile(r'Bat(wo)+man')
>>> mo1=batRegex.search('The Adventure of Batwoman')
>>> mo1.group()
'Batwoman'
>>> mo2=batRegex.search('The Adventure of Batwowowowoman')
>>> mo2.group()
'Batwowowowoman'
>>> mo3=batRegex.search('The Adventrue of Batman')
>>> mo3==None
True
7.3.6 用花括号匹配特定次数
>>> haRegex=re.compile(r'(Ha){3}')
>>> mo1=haRegex.search('HaHaHa')
>>> mo1.group()
'HaHaHa'
>>> mo2=haRegex.search('Ha')
>>> mo2==None
True
内容来源
[1] [美]斯维加特(Al Sweigart).Python编程快速上手——让繁琐工作自动化[M]. 王海鹏译.北京:人民邮电出版社,2016.7.p115-123