正則表達式(Regular Expression,簡稱 regex 或 regexp)是一種強大的文本處理工具,廣泛應用于字符串的搜索、匹配和替換操作。Python 通過內置的 re
模塊提供了對正則表達式的支持。本文將詳細介紹如何在 Python 中使用正則表達式,包括基本語法、常用函數以及一些實際應用場景。
正則表達式由一系列字符和特殊符號組成,用于定義字符串的匹配模式。以下是一些常用的正則表達式元字符:
.
:匹配任意單個字符(除了換行符)。^
:匹配字符串的開頭。$
:匹配字符串的結尾。*
:匹配前面的字符零次或多次。+
:匹配前面的字符一次或多次。?
:匹配前面的字符零次或一次。{n}
:匹配前面的字符恰好 n 次。{n,}
:匹配前面的字符至少 n 次。{n,m}
:匹配前面的字符至少 n 次,至多 m 次。[]
:匹配括號內的任意一個字符。|
:表示“或”操作,匹配左邊或右邊的表達式。()
:分組,將多個字符整體進行匹配。re
模塊Python 的 re
模塊提供了豐富的函數來處理正則表達式。以下是一些常用的函數:
re.match()
re.match()
函數用于從字符串的開頭開始匹配正則表達式。如果匹配成功,返回一個匹配對象;否則返回 None
。
import re
pattern = r"hello"
text = "hello world"
match = re.match(pattern, text)
if match:
print("Match found:", match.group())
else:
print("No match")
re.search()
re.search()
函數用于在字符串中搜索匹配正則表達式的第一個位置。與 re.match()
不同,re.search()
不要求匹配從字符串的開頭開始。
import re
pattern = r"world"
text = "hello world"
match = re.search(pattern, text)
if match:
print("Match found:", match.group())
else:
print("No match")
re.findall()
re.findall()
函數用于查找字符串中所有匹配正則表達式的子串,并返回一個列表。
import re
pattern = r"\d+"
text = "There are 3 apples and 5 oranges."
matches = re.findall(pattern, text)
print("Matches:", matches)
re.sub()
re.sub()
函數用于替換字符串中匹配正則表達式的子串。
import re
pattern = r"\d+"
text = "There are 3 apples and 5 oranges."
result = re.sub(pattern, "X", text)
print("Result:", result)
re.split()
re.split()
函數用于根據正則表達式分割字符串。
import re
pattern = r"\s+"
text = "Split this text by spaces."
result = re.split(pattern, text)
print("Result:", result)
正則表達式中的分組使用圓括號 ()
表示。分組不僅可以用于將多個字符整體進行匹配,還可以用于捕獲匹配的內容。
import re
pattern = r"(\d{4})-(\d{2})-(\d{2})"
text = "Date: 2023-10-05"
match = re.search(pattern, text)
if match:
print("Year:", match.group(1))
print("Month:", match.group(2))
print("Day:", match.group(3))
正則表達式默認是貪婪匹配,即盡可能多地匹配字符??梢酝ㄟ^在量詞后面加上 ?
來實現非貪婪匹配。
import re
# 貪婪匹配
pattern_greedy = r"<.*>"
text = "<html><head><title>Title</title></head></html>"
match_greedy = re.search(pattern_greedy, text)
print("Greedy match:", match_greedy.group())
# 非貪婪匹配
pattern_non_greedy = r"<.*?>"
match_non_greedy = re.search(pattern_non_greedy, text)
print("Non-greedy match:", match_non_greedy.group())
import re
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
email = "example@example.com"
if re.match(pattern, email):
print("Valid email")
else:
print("Invalid email")
import re
pattern = r"https?://(?:www\.)?\S+"
text = "Visit https://www.example.com for more info."
urls = re.findall(pattern, text)
print("URLs:", urls)
import re
pattern = r"(bad|naughty|evil)"
text = "This is a bad example."
result = re.sub(pattern, "***", text)
print("Result:", result)
Python 的 re
模塊提供了強大的正則表達式功能,能夠處理各種復雜的字符串操作。通過掌握正則表達式的基本語法和常用函數,可以大大提高文本處理的效率和靈活性。無論是數據清洗、日志分析還是文本挖掘,正則表達式都是一個不可或缺的工具。
希望本文能幫助你更好地理解和使用 Python 中的正則表達式。如果你有更多問題或需要進一步的幫助,請參考 Python 官方文檔或相關教程。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。