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()在整个字符串中搜索。

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

相关推荐
吃饱了得干活25 分钟前
别再手动解析 LLM 输出了!LangChain 四种结构化输出方案对比
后端·python·langchain
ikun_文29 分钟前
Python进阶—函数编程
python·pycharm
MC皮蛋侠客33 分钟前
uv 系列(三):依赖、锁文件与环境同步——可重复构建的核心
python·uv
量化吞吐机35 分钟前
2026年交易想法转Python,中间先补规则转译
人工智能·python
用户2986985301437 分钟前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
决战灬41 分钟前
langgraph之interrupt(事例篇)
人工智能·python·agent
IPdodo_1 小时前
Codex 总是 Reconnecting?从 401 到响应流中断的排查方法
python·requests
京和动物医院·总院1 小时前
2026年未央区宠物医院:如何挑选最适合您爱宠的健康守护者
大数据·人工智能·python
刘小八1 小时前
RAG 文档切分不是越细越好:选择 Chunk Size 与 Overlap
人工智能·python·语言模型
互联网中的一颗神经元1 小时前
小白python入门 - 23. Python 正则表达式的应用
python·正则表达式