Python 正则表达式 search vs match

  search()和match()函数都是正则表达式中的匹配函数,二者有何区别呢?

  1、match()从string的开始位置进行正则匹配,即从0位置开始匹配,若匹配上则返回一个match对象,否则返回none

import re
m=re.match('d{3}-d{3}-d{4}','My home phone number is 456-963-1125,My office number is 456-639-1125')
if m:
print(m.group(0))
else:
print('not matched')

运行结果是 not matched
  2、search()函数扫描整个string,寻找第一个正则匹配的位置,若找到,则返回一个match对象,否则返回none,同样是上面的例子,使用search()函数运行
import re
m=re.search('d{3}-d{3}-d{4}','My home phone number is 456-963-1125,My office number is 456-639-1125')
if m:
print(m.group(0))
else:
print('not matched')

运行结果是 456-963-1125
  3、可以在正则表达式前加^来强制search()函数从头开始匹配
import re
m=re.search('^d{3}-d{3}-d{4}','My home phone number is 456-963-1125,My office number is 456-639-1125')
if m:
print(m.group(0))
else:
print('not matched')

运行结果是 not matched
  4、MULTILINE模式下,match()函数只在第一行的string中进行匹配,而加了^限制的search()函数会在多行进行匹配
m=re.match('X', 'A
B
X', re.MULTILINE)
if m:
print(m.group(0))
else:
print('not matched')

运行结果是 not matched
m=re.search('^X', 'A
B
X', re.MULTILINE)
if m:
print(m.group(0))
else:
print('not matched')

运行结果是 X

  除了match()和search()外,findall()方法将返回被查找字符串中的所有匹配

1、如果调用在一个没有分组的表达式上,将返回一个匹配字符串列表

>>>
>>> phoneNumRegex=re.compile(r'd{3}-d{3}-d{4}')
>>> phoneNumRegex.findall('Home:021-364-8975 Office:021-876-6934')
['021-364-8975', '021-876-6934']
>>>

2、如果调用在一个有分组的表达式上,将返回一个匹配字符串的元组的列表

>>>
>>> phoneNumRegex=re.compile(r'(d{3})-(d{3}-d{4})')
>>> phoneNumRegex.findall('Home:021-364-8975 Office:021-876-6934')
[('021', '364-8975'), ('021', '876-6934')]
>>>

3、如果没有字符串中没有相应的匹配,将会返回一个空列表

>>>
>>> phoneNumRegex=re.compile(r'd{3}-d{3}-d{4}')
>>> phoneNumRegex.findall('Home:021-364-897 Office:021-876-693')
[]
>>>

原文地址:https://www.cnblogs.com/pigwan7/p/7813363.html