1.定义
正则表达式可以用来搜索、编辑或处理文本
2.表达式组件
()
括起来的部分就是要提取的。
.
匹配任何字符串。
+
一次或多次。
?
不要太贪婪,在找到第一个匹配项后停止
3.常用的字符集匹配模式
第一:'[pj]yhton'与'python'和'jython'都能匹配,[pj]表示第一个字符是p或者是j都可以匹配
第二:'[a-z]'与a~z的任何字母都匹配,还可以组合多个访问,'[a-zA-Z0-9]'与大写字母、小写字母、数字都匹配
第三:要指定排除的字符集,可在开头添加一个^字符,例'[^abc]'与除a、b、c外的其他任何字符都匹配
4.二选一和子模式
- 二选一模式
如果只想匹配字符串'python'和'perl',必须使用表示二选一的特殊字符:管道字符'|',所需模式为'python|perl'
- 子模式
有时候不想将二选一运算符用于整个模式,而只想将其用于模式的一部分。
为此,可将这部分(子模式)放在圆括号内,可以写成'p(ython|erl)'
- 子模式匹配情况
(pattern)?:表示pattern匹配模式可以出现0次或者1次
(pattern)*:表示pattern匹配模式可以出现0次、1次或多次
(pattern)+:表示pattern匹配模式可以出现1次或多次
(pattern){m,n}:表示pattern匹配模式可以重复m~n次
5.可选模式和重复模式
例如:r'(http://)?(www\.)?python\.org'(使用原始字符串r'xxxxxx',可以减少所需的转义斜杠数量)
会出现以下匹配结果:
1、http://www.python.org
2、www.python.org
3、http://python.org
4、python.org
6.python RE模块介绍
- 常用函数
函数名 | 介绍 |
---|---|
compile(pattern[,flags]) | 根据包含正则表达式的字符串创建模式对象,返回一个匹配模式的对象 |
search(pattern,string[,flags]) | 在字符串中查找第一个与指定正则表达式匹配的子串,匹配成功返回MacthObject(结果为真),否则返回None(结果为假) |
match(pattern,string[,flags] | 在字符串开头查找与正则表达式匹配的子串,匹配成功返回MacthObject(结果为真),否则返回None(结果为假)tips:如果要求与整个字符串匹配,需要在模式末尾加上一个$,将匹配模式延伸到整个字符串 |
split(pattern,string[,maxsplit=0]) | 根据匹配模式来分割字符串,比字符串split更加灵活 |
findall(pattern,string) | 返回一个列表,其中包含所有与给定模式匹配的子串 |
sub(pat,repl,string[,flags]) | 将字符串中于模式pat匹配的子串都替换为repl(从左往右) |
escape(string) | 对字符串中所有可能被视为正则表达式运算符的字符进行转义 |
- 示例
- re.compile
import re
set_string='The sum of 7 and 9 is [7+9]'
pat=re.compile(r'\[(.+?)\]')
- re.search
import re
set_string='The sum of 7 and 9 is [7+9]'
pat=re.compile(r'\[(.+?)\]')
s=re.search(pat,set_strint)
print(s)
-------------------------------------------------------------------------------------------
<_sre.SRE_Match object; span=(22, 27), match='[7+9]'>
- re.match
import re
set_string='The sum of 7 and 9 is [7+9]'
pat=re.compile(r'\[(.+?)\]')
s=re.match(pat,set_strint)
print(s)
-------------------------------------------------------------------------------------------
None
- re.split
import re
set_string='The sum of 7 and 9 is [7+9]'
pat=re.compile(r' ')
get_list=re.split(pat,set_string)
print(get_list)
-------------------------------------------------------------------------------------------
['The', 'sum', 'of', '7', 'and', '9', 'is', '[7+9]']
- re.findall
import re
set_string='The sum of 7 and 9 is [7+9],and the sum of 9 and 9 is [9+9]'
pat=re.compile(r'\[(.+?)\]')
s=re.findall(pat,set_string)
print(s)
-------------------------------------------------------------------------------------------
['7+9', '9+9']
- re.sub
import re
set_string='The sum of 7 and 9 is [7+9],and the sum of 9 and 9 is [9+9]'
pat=re.compile(r'\[(.+?)\]')
s=re.sub(pat,'result',set_string)
print(s)
-------------------------------------------------------------------------------------------
'The sum of 7 and 9 is result,and the sum of 9 and 9 is result
---------------------
- re.eacape
import re
set_string=r'https://www.python.org'
get_string=re.escape(set_string)
print(get_string)
-------------------------------------------------------------------------------------------
'https\:\/\/www\.python\.org'
7.贪婪模式和非贪婪模式
-
贪婪模式
重复运算符默认是贪婪的,这意味着它们将匹配尽可能多的内容。
import re
set_string=r'*something**getlalala**wulalala*'
repl=r'<em>\1<\em>'
pat=re.compile('\*(.+)\*')
get_string=re.sub(pat,repl,set_string)
print(get_string)
-------------------------------------------------------------------------------------------
'<em>something**getlalala**wulalala<\em>'
-
非贪婪模式
对于所有的重复运算符,都可在后面加上问号来将其指定为非贪婪模式。匹配会尽可能少的内容。
import re
set_string=r'*something**getlalala**wulalala*'
repl=r'<em>\1<\em>'
pat=re.compile('\*(.+?)\*')
get_string=re.sub(pat,repl,set_string)
print(get_string)
-------------------------------------------------------------------------------------------
'<em>something<\em><em>getlalala<\em><em>wulalala<\em>'
网友评论