Python正则-match,search,findall区别&实例解析

Content 

match
匹配string 开头,成功返回Match object, 失败返回None,只匹配一个

search
在string中进行搜索(而不是局限于开头),成功返回Match object, 失败返回None, 只匹配一个

findall
在string中查找所有 匹配成功的组, 即用括号括起来的部分。返回list对象,每个list item是由每个匹配的所有组组成的list。

1. match

re.match() 总是从字符串“开头”去匹配,并返回匹配的字符串的match对象。所以当我用re.match()函数去匹配字符串非开头部分的字符串时,会返回NONE。

e.g.1

实例如下,只有‘string3’可以打印出结果p,其他都是输出‘NONE’。

import re

string1='I love python but hate pig'
string2='I love python'
string3='python'
string4='123'
result = re.match(r'[p]', string1)
print(result)
import re
 
# 将正则表达式编译成Pattern对象
pattern = re.compile(r'hello')
 
# 使用Pattern匹配文本,获得匹配结果,无法匹配时将返回None
match = pattern.match('hello world, hello word')
 
if match:
    # 使用Match获得分组信息
    print (match.group())


hello

直观来说,re.match()用途很有限。匹配string 开头只匹配一个

看个人需求,有时候该方法也挺有用。接下来是大量扩展,介绍多种匹配模式。

1.1 匹配a到z之间的字符

string3='python'
string4='123'
result = re.match(r'[a-z]', string3)
print(result) # p

1.2 匹配A到Z之间的字符

string3='Python'
string4='123'
result = re.match(r'[A-Z]', string3)
print(result) # P

1.3 匹配0到9之间的字符

ma = re.match(r'[0-9]',string4) 
print (ma.group())

1.4 a-z、A-Z和0-9可组合使用

string3='python'
string4='123'
result = re.match(r'[a-zA-Z0-9]', string3)
print(result)

\w 和 \W 同上,分别匹配单词字符[a-zA-Z0-9]和非单词字符 。

1.5 匹配数字/非数字

string4 = '[];;:'
ma1 = re.match(r'\D',string4)#匹配非数字
ma2 = re.match(r'\d',string2)#匹配数字
print (ma1.group())    # [
# print (ma2.group())  # raise error

1.6 匹配空白和非空白字符

\s 和 \S 同上,分别匹配空白和非空白字符.

1.7 匹配0到无穷次:  * (星号)

ma = re.match(r'[a-z][a-z]*',string1)

1.8 匹配1到无穷次: + (加号)

1.9 匹配出现m到n次的字符串:{m,n} 

ma = re.match(r'[\w]{1,4}',string1)任意字母和数字出现1到4次

2. search

在string中进行搜索(而不是局限于开头),成功返回Match object, 失败返回None, 只匹配一个

通配符 还是和之前的一样。

如需了解更多通配符,可参考下面的blog:

https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

e.g.

import re
string5 = '[email protected]'
ma6 = re.search(r'[\d]+',string5)  #匹配数字
print(ma6)
print(ma6.group())


output:
<_sre.SRE_Match object; span=(9, 12), match='676'>
676
import re

string1='I love python but hate pig'
string2='I love python'
string3='python'
string4='123'
result = re.search(r'[\w]+', string3)
print(result)  # python

result2 = re.search(r'[\w]+', string2)
print(result2)  # I

注意,输入为string2时,search到的结果是‘I’。因为‘I’和‘love’之间是空格(非字符),因此\w无法识别。

直白说,遇到通配符无法识别的字符,search就结束了,因为只返回一个结果。

string4='123 45'
result = re.search(r'[\d]+', string4)
print(result)  # 123

当然,也可以强行这样做,匹配所有字符。 

str2 = 'char|johljh'
ma6 = re.search(r'char[\W][\w]+',str2)
print(ma6.group())

char|johljh
str = 'oajfs|char|dhddfgdfg'
str2 = 'char|johljh|jjgkhk'
str3 = 'dlkngldnfk|flmgkdm|char'

ma6 = re.search(r'char[\W][\w]+',str) #此pattern可以匹配到str str2
print(ma6.group())

ma6 = re.search(r'[\W]char',str3) # 此pattern 可以匹配到str3
print(ma6.group())

3. findall

在string中查找所有 匹配成功的组, 即用括号括起来的部分。返回list对象,每个list item是由每个匹配的所有组组成的list。

import re

string1='I love python but hate pig'
string2='I love python'
string3='python'
string4='123 45'

result0 = re.findall(r'[p]+', string1)
result1 = re.findall(r'[p][a-z]+', string1)
result2 = re.findall(r'[\w]+', string2)
result4 = re.findall(r'[\d]+', string4)

print(result0)
print(result1)
print(result2)
print(result4)

output:

['p', 'p']
['python', 'pig']    --->  highly recommend       result1 = re.findall(r'[p][a-z]+', string1)
['I', 'love', 'python']
['123', '45']
string2 = '1,2,3,4'
ma = re.findall(r'\d+',string2)
print (ma)

#['1', '2', '3', '4']
import re
 
p = re.compile(r'\d+')
print (p.findall('one1two2three3four456'))
 
### output ###
# ['1', '2', '3', '456']

References:

https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

https://blog.csdn.net/ali197294332/article/details/50894419

https://blog.csdn.net/tp7309/article/details/72823258

https://blog.csdn.net/djskl/article/details/44357389

发布了18 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Zhou_Dao/article/details/103943192