python 正则表达式

正则表达式(re)

正则表达式是可以匹配文本片段的模式,可以用来匹配行为搜索文本中的模式,并且用计算后的值替换特定模式,或者将文本进行分段。

预定义字符串集:
d     数字[0-9]
D    非数字:[^d]
s      空白字符
S    非空白字符:[^s]
w    单词字符:[a-zA-Z0-9]
W    非单词字符:[^w]

匹配范围:
.      匹配单个任意字符
[...]        匹配某个范围内的某个字符
*      匹配前一个字符0或多次
+      匹配前一个字符1次或多次
?     匹配前一个字符0次货1次
{m}      匹配前一个字符m次
{m,n}     匹配前一个字符m至n次
^      匹配以某个字符串开头
$      匹配以某个字符串结尾
A      匹配仅以字符串开头
      匹配仅以字符串结尾
      匹配除数字与字母以外的字符
B      仅匹配数字与字母
|         匹配前、后中任意一个内容
(...)     表示分组匹配


re模块中一些重要的函数:
compile(pattern[,flags]) 根据包含正则表达式的字符串创建模式对象(pattern:一个字符串形式的表达式,flags:可选,表示匹配模式,比如忽略大小写,多行模式等。具体参数:
1、re.I:忽略大小写
2、re.L:表示特殊字符集w,W,,B,s,S 依赖于当前环境
3、re.M: 多行模式
4、re.S:即为,并且包括换行符在内的任意字符(不包括换行符)
5、re.U:表示特殊字符集w,W,,B,s,S依赖于unicode字符属性数据库
6、re.X:为了增加可读性,忽略空格和#后面的注释)

search(pattern,string[,flags]) 在字符串中寻找模式

match(pattern,string[,flags]) 在字符串的开始处匹配模式

split(pattern,string[,maxsplit=0]) 根据模式的匹配项来分隔字符串(maxsplit:分隔次数,maxsplit=1表示,分隔1次,默认为0,不限次数)

findall(pattern,string[,pos[,endpos]]) 列出字符串中模式的所有匹配项(string:待匹配的字符串,pos:可选参数,指定字符串的起始位置,默认为0,endpos:可选参数,指定字符串的结束位置)

finditer(pattern,string)列出字符串中模式的匹配项,返回一个迭代器

sub(pat,repl,string[,count=0]) 将字符串中所有的pat的匹配项用repl替换(pattern:正则表达式中的模式字符串,repl:替换的字符串,也可以是一个函数,string:要被查找替换的原始字符串,count:模式匹配后替换的最大次数,默认0表示替换所有的匹配)

escape(string) 将字符串中所有特殊正则表达式字符串转义

匹配对象方法:
group(num=0) 匹配的整个表达式的字符串,group()可以一次输入多个组好,在这种情况下它将返回一个包含那些组所对应值的元组
groups() 返回一个包含所有小组字符串的元组,从1到所有的小组号

以下是各方法实例:

compile()方法,用于创建正则表达式字符串对象
import re
pattern=re.compile(r"python")			#正则表达式的字符串创建模式对象
result=pattern.match("python welcome you ").span()     #在开始位置匹配,span()方法返回匹配的位置
print result
运行结果:
(0, 6)
[Finished in 0.1s]

match()方法,用于在开始位置匹配
pattern=re.compile(r"python")			#正则表达式的字符串创建模式对象
result=pattern.match("python welcome you ").group()  #在开始位置匹配,group()方法返回匹配结果
print result
运行结果:
python
[Finished in 0.2s]

group()
str_a="tHIS is python home ,welcome you"
pattern=re.compile(r't(.*) is py(.+) .*',re.I)
result=pattern.match(str_a)    #在开始位置匹配
print result.group()
print result.group(1)    #打印第一组的匹配内容
print result.group(2)	 #打印第二组的匹配内容
运行结果:
tHIS is python home ,welcome you
HIS
thon home ,welcome
[Finished in 0.2s]


search()方法,用于扫描整个字符串,并返回第一个成功的匹配
str_a="this is python home ,welcome you"
pattern=re.compile(r'(is)',re.I)
result=pattern.search(str_a)
print result.span()    #打印第一个匹配成功的位置
print result.group()运行结果:
(2, 4)    
is
[Finished in 0.1s]


sub()方法,用于替换匹配的字符串
str_a="this is python home ,welcome you"
pattern=re.compile(r",.*$")   #匹配逗号后面的所有内容
result=re.sub(pattern,"",str_a)  #用空格替换匹配到的内容
print result
运行结果:
this is python home 
[Finished in 0.1s]



findall()方法,用于在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
str_a="this is python home ,welcome you"
pattern=re.compile(r"(is)")   
result=re.findall(pattern,str_a)  #匹配所有含is的内容,并以列表形式返回
print result
运行结果:
['is', 'is']
[Finished in 0.3s]


finditer()方法,用于字符串中找到正则表达式所匹配的所有子串,并把它们作为迭代器返回
str_a="this is python home ,welcome you"
pattern=re.compile(r"(is)")   #匹配逗号后面的所有内容
result=re.finditer(pattern,str_a)  #用空格替换匹配到的内容
print result
运行结果:
<callable-iterator object at 0x021916F0>
[Finished in 0.2s]

split()方法,用于根据模式的匹配项来分隔字符串。并以列表形式返回
str_a="this is python home ,welcome you"
pattern=re.compile(r",")   
result=re.split(pattern,str_a)  #匹配含逗号的地方,并以此来分隔字符串,并以列表形式返回
print result
运行结果:
['this is python home ', 'welcome you']
[Finished in 0.3s]

  

原文地址:https://www.cnblogs.com/JcHome/p/10129085.html