Python 的 re.search 和 re.match 有什麼區別?


在廣闊的 Python 程式設計領域中,正則表示式是文字處理和模式匹配的重要工具,為開發者提供了無限可能。在 Python 的 re 模組中,re.search() 和 re.match() 這兩個函式是字串模式匹配的佼佼者。雖然這兩個函式都致力於模式搜尋,但它們的行為和執行方式卻有所不同。在這篇全面且富有啟發性的文章中,我們將逐步深入探討 Python 的 re.search() 和 re.match() 函式的細節。透過一些實際的程式碼示例和清晰的解釋,我們旨在幫助您深刻理解這兩個強大工具之間細微的差別。

理解 Python 中正則表示式的強大功能

在我們深入研究 re.search() 和 re.match() 之前,務必掌握 Python 領域中正則表示式(通常稱為“regex”)的本質。這些強大的工具使開發人員能夠與字串進行復雜的互動,利用其強大的功能根據特定模式匹配和操作字串。藉助一系列特殊字元和符號,正則表示式定義了字串集,從而開啟了高階文字搜尋、替換和驗證的新時代。

re.search() 函式的強大功能

re.search() 函式是一個強大的助手,它可以搜尋指定字串中給定模式。它會掃描整個輸入字串,查詢指定模式的第一次出現。當找到所需的模式時,re.search() 會向開發者提供一個匹配物件,作為第一次成功匹配的證據。但是,如果模式不存在,re.search() 的輸出為 None。

使用 re.search()

示例

考慮一個名為“search_pattern_in_string”的特殊函式。它使用由特殊字元和文字組成的模式。此函式使用“re.search()”來實現這一目標。它仔細檢查文字以查詢指定的模式。在我們的例子中,程式碼查詢一個或多個數字的組。當我們用特定文字嘗試此函式時,它會在文字中顯示模式“100”,並打印出已找到該模式。

import re

def search_pattern_in_string(pattern, text):
   match = re.search(pattern, text)
   if match:
      print(f"Pattern '{pattern}' found in the text.")
   else:
      print(f"Pattern '{pattern}' not found in the text.")

# Example usage
pattern = r'\d+'
text = "The price of the product is $100."
search_pattern_in_string(pattern, text)

輸出

Pattern '\d+' found in the text.

使用 re.match() 函式

另一方面,還有一個名為“re.match()”的強大工具。它採用了一種獨特的方法來工作。此函式只在文字的開頭查詢所需的模式。如果在開頭找到該模式,則返回一個匹配物件。但是,如果在開頭未找到該模式,re.match() 將返回“None”,表示該模式不存在。

示例

讓我們考慮一下 match_pattern_at_start 函式,它接受模式和文字作為引數。在 re.match() 的幫助下,此函式在文字的開頭搜尋隱藏的模式。特殊程式碼“r'\d+'”顯示了一個或多個數字的存在。當我們用特定文字呼叫此函式時,模式“100”未在文字開頭找到,因此它會打印出函式未找到該模式。

import re

def match_pattern_at_start(pattern, text):
   match = re.match(pattern, text)
   if match:
      print(f"Pattern '{pattern}' found at the start of the text.")
   else:
      print(f"Pattern '{pattern}' not found at the start of the text.")

# Example usage
pattern = r'\d+'
text = "100 is the price of the product."
match_pattern_at_start(pattern, text)

輸出

Pattern '\d+' found at the start of the text.

re.search() 和 re.match() 之間的區別

現在我們已經見證了 re.search() 和 re.match() 函式的工作方式,讓我們嘗試闡明這兩個函式之間的主要區別:

搜尋範圍

  • re.search() − 掃描整個輸入字串以查詢模式。

  • re.match() − 將其搜尋限制在輸入字串的開頭。

匹配位置

  • re.search() − 查詢模式的第一次出現,無論其位置如何。

  • re.match() − 只有當在輸入字串的開頭髮現模式時才允許匹配。

返回值

  • re.search() − 如果找到模式,則返回匹配物件;否則,返回 None。

  • re.match() − 如果在開頭找到模式,則返回匹配物件;否則,返回 None。

效能

  • re.search() − 對整個輸入字串進行徹底搜尋,可能會影響大型字串的效能。

  • re.match() − 透過僅關注在字串開頭匹配模式而提高效率。

利用 re.search() 進行多次匹配

re.search() 的另一個特點是它對輸入字串中多次匹配的響應。當函式找到第一次出現時,它會與 re.match() 不同,re.match() 只在開頭搜尋模式。

現在讓我們學習 re.search() 如何處理多次匹配:

示例

讓我們來看一下 search_multiple_matches 函式,它以正則表示式模式和文字作為輸入變數。透過使用 re.search(),此函式開始其重要任務,在給定文字中搜索隱藏的模式。“r'\d+'”模式表示一個或多個數字。當我們使用此函式和特定文字時,就會發生神奇的事情——它在文字索引 19 處找到模式“100”。

import re

def search_multiple_matches(pattern, text):
   matches = re.search(pattern, text)
   if matches:
      print(f"Pattern '{pattern}' found at index {matches.start()}.")

# Example usage
pattern = r'\d+'
text = "The product costs $100. The discounted price is $50."
search_multiple_matches(pattern, text)

輸出

Pattern '\d+' found at index 19.

使用 re.match() 進行多次匹配

現在讓我們進入 re.match() 的領域,因為它在搜尋開頭模式時會顯示其對多次匹配的響應。

示例

觀察 match_multiple_matches_at_start 函式,它以正則表示式模式和文字字串作為引數。在 re.match() 的幫助下,此函式開始一項任務,在文字開頭搜尋所需的模式。“r'\d+'”模式表示一個或多個數字。當我們用示例文字呼叫此函式時,模式“100”仍然難以捉摸,並且它會打印出未找到該模式。

import re

def match_multiple_matches_at_start(pattern, text):
   matches = re.match(pattern, text)
   if matches:
      print(f"Pattern '{pattern}' found at index {matches.start()}.")

# Example usage
pattern = r'\d+'
text = "100 is the product code. 200 is the order code."
match_multiple_matches_at_start(pattern, text)

輸出

Pattern '\d+' found at index 0.

使用標誌增強 re.search()

re.search() 的附加功能在於它能夠呼叫標誌,從而實現不區分大小寫的匹配、多行搜尋和其他選項。讓我們用一個包含不區分大小寫標誌的示例來說明這一點:

示例

讓我們考慮一下 case_insensitive_search 函式,它以正則表示式模式和文字字串作為引數。在 re.search() 的幫助下,此函式開始搜尋指定模式的任務,同時呼叫不區分大小寫標誌。“r'text'”模式匹配單詞“text”。當我們用給定的示例文字呼叫該函式時,它將使用不區分大小寫標誌找到模式“TEXT”,並打印出已找到該模式。

import re

def case_insensitive_search(pattern, text):
   matches = re.search(pattern, text, re.IGNORECASE)
   if matches:
      print(f"Pattern '{pattern}' found in the text.")

# Example usage
pattern = r'text'
text = "This is a TEXT example."
case_insensitive_search(pattern, text)

輸出

Pattern 'text' found in the text.

在比較 re.search() 和 re.match() 時,我們見證了兩個不同但相互關聯的函式的實用性,每個函式都具有其獨特的功能。理解它們的細微之處對於巧妙地進行文字處理至關重要。當我們將 re.search() 和 re.match() 的知識納入我們的 Python 知識體系時,我們將能夠自信地應對各種文字處理挑戰,無論是查詢第一次出現、在開頭匹配還是進行不區分大小寫的搜尋。Python 的正則表示式為處理複雜的文字匹配和操作任務提供了強大的工具包。有了這些強大的工具,我們就可以充滿信心地前進,揭開 Python 領域中文字處理的無限奇蹟。

更新於:2023年8月22日

989 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.