python 的正则表达式指北
正则表达式用来拆分字符串
>>> s = 'one1two2three3four4'
>>> pattern = re.compile(r'\d+')
>>> for v in pattern.split(s):
... print(v)
...
one
two
three
four
正则表达式查找看上去是跟 split 相反的功能
pattern = re.compile(r'\d+')
for match in pattern.findall('one1two2three3four4'):
print(match)
正则表达式查找返回迭代器
pattern = re.compile(r'\d+')
for index,match in enumerate(pattern.finditer('one1two2three3four4')):
print(index,'--->',match.group())
暂时这样写
>>> pattern = re.compile(r'([a-zA-Z]+)(\d{3})([bcd]{3})',0)
>>> match = re.match(pattern,s)
>>> if match:
... for m in match.groups():
... print(m)
match 与 search 的 区别, match 从 0 下标开始匹配模式没匹配上则 返回 None , 而 search 则会遍历完整个字符串并返回第一个匹配到的值
## 这里是可以返回 234 的, 而把 search 替换为 match 则会报错
import re
>>> s = 'abc234bdli23243afsdf'
>>> p1 = re.compile('(\d+)')
>>> match1 = re.search(p1,s)
>>> for m in match1.groups():
... print(m)
正则拆分字符串
>>> s = 'ab c d e'
>>> str_list = re.split('\s+',s)
>>> for str in str_list:
... print(str)
...
ab
c
d
e
邮件拆分
>>> email = 'bill.gates@microsoft.com'
>>> pattern = re.compile('([\w]+)(\.?)([\w]+)@([\w\.]+)')
>>> match = re.match(pattern,email)
>>> print(match.groups())
('bill', '.', 'gates', 'microsoft.com')
邮件地址拆分二
# <Tom Paris> tom@voyager.org => Tom Paris
# bob@example.com => bob
# -*- coding: utf-8 -*-
import re
def name_of_email(addr):
pattern = re.compile(r'<?([a-zA-Z\s]+)>?.*@[\w\.]+')
match = re.match(pattern,addr)
rs = None
if match:
rs = match.group(1)
return rs
print(name_of_email('<Tom Paris> tom@voyager.org'))
# 测试:
assert name_of_email('<Tom Paris> tom@voyager.org') == 'Tom Paris'
assert name_of_email('tom@voyager.org') == 'tom'
print('ok')
向前向后查找
import re
key = r"<html><body><h1>hello world</h1></body></html>"#这段是你要匹配的文本
p1 = r"(?<=<h1>).*?(?=</h1>)"#这是我们写的正则表达式规则,你现在可以不理解啥意思
pattern1 = re.compile(p1)#我们在编译这段正则表达式
matcher1 = re.search(pattern1,key)#在源文本中搜索符合正则表达式的部分
print(matcher1.group(0))#打印出来
回溯查找
import re
key = r"<h1>hello world</h1>"
pattern = re.compile(r'(?<=<h([1-6])>).*?(?=</h\1>)') # 综合理解
match = re.search(pattern,key)
if match:
print(match.group(0))
手机号 查找
import re
txt = 'abc13975041239ljlboou'
phone_pattern = re.compile(r'1[39|38|58]\d{8}')
grp = re.findall(phone_pattern,txt)
grp2 = phone_pattern.findall(txt)
print(grp)
print(grp2)
拆分电话号码2
import re
txt = 'abc13975041239ljlboou'
phone_pattern = re.compile(r'1[39|38|58]\d{8}')
grp = re.findall(phone_pattern,txt)
grp2 = phone_pattern.findall(txt)
grp3 = phone_pattern.search(txt)
grp4 = phone_pattern.finditer(txt)
print(grp)
print(grp2)
print(grp3.group(0))
grp5 = [g for g in grp4]
print(grp5)
分组
import re
contactInfo = 'Doe, John: 555-1212'
pattern = re.compile(r'(?P<first>[a-zA-Z]+),\s+(?P<second>\w+):\s+([\d-]+)')
match = pattern.match(contactInfo)
if match:
print(match.group())
print(match.group(1))
print(match.group(2))
print(match.group(3))
仅起到分组 取别名作用 ?P
import re
contactInfo = 'Doe, John: 555-1212'
pattern = re.compile(r'(?P<first>[a-zA-Z]+),\s+(?P<second>\w+):\s+(?P<phone>[\d-]+)')
match = pattern.match(contactInfo)
if match:
print(match.group())
print(match.group('first'))
print(match.group('second'))
print(match.group('phone'))
>>> contactinfo = 'Doe, John: 555-1212'
>>> pattern = r'(?P<first>\w+),\s+(?P<second>\w+):\s+(?P<phone>[\d-]+)'
>>> match = re.match(pattern,contactinfo)
>>> match.groups()
('Doe', 'John', '555-1212')
>>> pattern = r'([a-zA-Z]+),\s+([a-zA-Z]+):\s+([\d-]+)'
>>> match = re.match(pattern,contactinfo)
>>> match.groups()
('Doe', 'John', '555-1212')
如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。