一. 正则表达式
1. 概述
- 概念
(1)Regular Expression
(2) 一种文本模式,描述在搜索文本时要匹配一个或多个字符串 - 典型场景
(1)数据验证
(2)文本扫描
(3)文本提取
(4)文本替换
(5)文本分割 - 语法
(1)字面值:普通字符,需转义(,^,$…)
(2)元字符 - 匹配
(1)单字,预定义元字符:. 除/n外所有字符,\d数字,\D 所有非数字, \s 空白字符,\S 非空白字符,\w 字母数字字符,\W 非字线数字
(2)批量备选:| yes|no
(3) 量词(字符、元字符,字符集如何重复):?0或1次,* 0或多次,+1或多次,特定{}, {n} n次,{n,}至少n次,{,m}最多m次
(3)贪婪或非贪婪:贪婪(尽量匹配最大的范围),非贪婪(尽量匹配最小范围,*后加?)
(4)边界匹配:^行首,$行尾,\b单词边界
2. Python 正则
1.模块
(1)import re
import re
text = 'Tom is 8 years old. Mike is 25 years old.'
pattern = re.compile('\d+')
pattern.findall(text)
['8', '25']
re.findall('\d',text)
['8', '2', '5']
- RegexDbject
(1) 模式对象,表现编译后的正则表达式
(2)编译: re.compile(r’模式’)
s = '\\author:Tom'
pattern = re.compile('\\author')
pattern.findall(s)
[]
pattern = re.compile('\\\\author')
pattern.findall(s)
['\\author']
pattern = re.compile(r'\\author')
pattern.findall(s)
['\\author']
(3)findall():查找所有非重叠匹配,返回list
text = 'Tom is 8 years old. Mike is 23 years old. Peter is 87 years old.'
pattern = re.compile(r'\d+')
pattern.findall(text)
['8', '23', '87']
p_name = re.compile(r'[A-Z]\w+')
p_name.findall(text)
['Tom', 'Mike', 'Peter']
(4).match(string[,pos[,endpos]]):匹配,仅从起始位置, 返回 HatchObject
text = '<html><head></head><body></body></html>'
pattern.match(text)
<_sre.SRE_Match object; span=(0, 6), match='<html>'>
text2 = ' <html><head></head><body></body></html>'
pattern.match(text2)
pattern.match(text2,1)
<_sre.SRE_Match object; span=(1, 7), match='<html>'>
(5).search(string[,pos[,endpos]]): 任意位置搜索,返回 HatchObject
text = 'Tom is 8 years old. Mike is 23 years old. Peter is 87 years old.'
p1 = re.compile(r'\d')
p2 = re.compile(r'[A-Z]\w+')
p1.match(text)
p2.match(text)
<_sre.SRE_Match object; span=(0, 3), match='Tom'>
p1.search(text)
<_sre.SRE_Match object; span=(7, 8), match='8'>
(6) .finditer(): 返回可迭代对象,返回 HatchObject
text
'Tom is 8 years old. Mike is 23 years old. Peter is 87 years old.'
p1
re.compile('\\d')
p1.findall(text)
['8', '2', '3', '8', '7']
it = p1.finditer(text)
for m in it:
..</