Python面试宝典7 | 正则表达式的match()与search(),精准匹配与全局搜索

今天,我们来聊聊Python正则表达式中两个常用的方法:match()search()。它们都用于在字符串中查找匹配的模式,但有着重要的区别。

理论篇:匹配的起始位置

match()search()最主要的区别在于它们匹配的起始位置:

  • match() 尝试从字符串的起始位置 匹配模式。如果匹配成功,则返回一个匹配对象;否则,返回None。也就是说,如果模式不是出现在字符串的开头,match()就无法匹配。
  • search() 在整个字符串中搜索第一个匹配的模式。如果匹配成功,则返回一个匹配对象;否则,返回None。也就是说,模式可以出现在字符串的任何位置。

代码篇:实例演示差异

让我们通过一些代码示例来清晰地展示它们的区别:

python 复制代码
import re

string = "hello world"

# 使用match()
match_result_1 = re.match("hello", string)
match_result_2 = re.match("world", string)

if match_result_1:
    print(f"match() 匹配成功:{match_result_1.group()}") # 输出 match() 匹配成功:hello
else:
    print("match() 匹配失败")

if match_result_2:
    print(f"match() 匹配成功:{match_result_2.group()}")
else:
    print("match() 匹配失败") # 输出 match() 匹配失败

# 使用search()
search_result_1 = re.search("hello", string)
search_result_2 = re.search("world", string)

if search_result_1:
    print(f"search() 匹配成功:{search_result_1.group()}") # 输出 search() 匹配成功:hello
else:
    print("search() 匹配失败")

if search_result_2:
    print(f"search() 匹配成功:{search_result_2.group()}") # 输出 search() 匹配成功:world
else:
    print("search() 匹配失败")


string2 = "oh my world"
match_result_3 = re.match("world", string2)
search_result_3 = re.search("world", string2)

if match_result_3:
    print(f"match() 匹配成功:{match_result_3.group()}")
else:
    print("match() 匹配失败") # 输出 match() 匹配失败

if search_result_3:
    print(f"search() 匹配成功:{search_result_3.group()}") # 输出 search() 匹配成功:world
else:
    print("search() 匹配失败")

在这个例子中:

  • 使用match()匹配"hello"时,因为"hello"在字符串的开头,所以匹配成功。而匹配"world"时,由于"world"不在开头,所以匹配失败。
  • 使用search()匹配"hello"和"world"时,都能成功匹配,因为它会在整个字符串中搜索。
  • 使用match()匹配"oh my world"中的"world"时,由于"world"不在开头,所以匹配失败。而使用search()则匹配成功。

返回值:匹配对象

无论是match()还是search(),如果匹配成功,都会返回一个匹配对象(Match Object)。这个对象包含了一些有用的方法和属性,例如:

  • group():返回匹配的字符串。
  • start():返回匹配的起始位置。
  • end():返回匹配的结束位置。
  • span():返回一个包含匹配的起始和结束位置的元组。

如果没有匹配成功,则返回None

python 复制代码
import re

string = "hello world"
match_result = re.match("hello", string)

if match_result:
    print(f"匹配的字符串:{match_result.group()}") # 输出 匹配的字符串:hello
    print(f"起始位置:{match_result.start()}") # 输出 起始位置:0
    print(f"结束位置:{match_result.end()}") # 输出 结束位置:5
    print(f"起始和结束位置:{match_result.span()}") # 输出 起始和结束位置:(0, 5)

使用场景

  • match() 适用于需要严格匹配字符串开头的情况,例如验证字符串是否以某个特定的前缀开头。
  • search() 适用于需要在字符串中查找某个模式的任何位置的情况,例如在一段文本中查找特定的关键词。

总结篇:精准定位,灵活搜索

match()search()是Python正则表达式中两个重要的匹配方法。理解它们的区别,可以帮助我们更精准地进行字符串匹配和处理。记住:

  • match()从字符串的开头匹配。
  • search()在整个字符串中搜索。

如果觉得不错,随手点个赞吧,如果想第一时间收到推送,也可以关注下我~谢谢你看我的文章,我们,下次再见。

相关推荐
权泽谦4 分钟前
用 Flask + OpenAI API 打造一个智能聊天机器人(附完整源码与部署教程)
python·机器人·flask
njxiejing12 分钟前
Numpy一维、二维、三维数组切片实例
开发语言·python·numpy
lskisme1 小时前
springboot maven导入本地jar包
开发语言·python·pycharm
开心-开心急了2 小时前
pyside6实现win10自动切换主题
开发语言·python·pyqt·pyside
mortimer2 小时前
一键实现人声伴奏分离:基于 `uv`, `FFmpeg` 和 `audio-separator` 的高效解决方案
python·ffmpeg·音视频开发
Sunhen_Qiletian3 小时前
Python 类继承详解:深度学习神经网络架构的构建艺术
python·深度学习·神经网络
程序员大雄学编程3 小时前
用Python来学微积分34-定积分的基本性质及其应用
开发语言·python·数学·微积分
Q_Q5110082853 小时前
python+django/flask的莱元元电商数据分析系统_电商销量预测
spring boot·python·django·flask·node.js·php
林一百二十八3 小时前
Python实现手写数字识别
开发语言·python
Q26433650234 小时前
【有源码】基于Hadoop+Spark的起点小说网大数据可视化分析系统-基于Python大数据生态的网络文学数据挖掘与可视化系统
大数据·hadoop·python·信息可视化·数据分析·spark·毕业设计