正則表達式(Regular Expression,簡稱 regex 或 regexp)是一種強大的文本處理工具,廣泛應用于字符串匹配、搜索、替換等操作。Python 通過 re
模塊提供了對正則表達式的支持。本文將介紹 Python 正則表達式中常見的知識點,幫助讀者快速掌握其基本用法。
正則表達式由普通字符和特殊字符(元字符)組成。普通字符匹配自身,而元字符則具有特殊的匹配規則。以下是一些常見的元字符:
.
:匹配任意單個字符(除了換行符 \n
)。^
:匹配字符串的開頭。$
:匹配字符串的結尾。*
:匹配前面的字符零次或多次。+
:匹配前面的字符一次或多次。?
:匹配前面的字符零次或一次。{n}
:匹配前面的字符恰好 n 次。{n,}
:匹配前面的字符至少 n 次。{n,m}
:匹配前面的字符至少 n 次,至多 m 次。[]
:匹配括號內的任意一個字符。|
:表示“或”操作,匹配左邊或右邊的表達式。()
:分組,將多個字符整體進行匹配。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)
正則表達式中的分組使用圓括號 ()
表示。分組不僅可以用于將多個字符整體進行匹配,還可以用于捕獲匹配的內容。
捕獲分組會將匹配的內容保存下來,可以通過 group()
方法訪問。
import re
pattern = r"(\d+)-(\d+)-(\d+)"
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 = r"(?:\d+)-(\d+)-(\d+)"
text = "Date: 2023-10-05"
match = re.search(pattern, text)
if match:
print("Month:", match.group(1))
print("Day:", match.group(2))
正則表達式默認是貪婪匹配的,即盡可能多地匹配字符??梢酝ㄟ^在量詞后面加上 ?
來實現非貪婪匹配。
import re
pattern = r"<.*>"
text = "<html><head><title>Title</title></head></html>"
match = re.search(pattern, text)
if match:
print("Greedy match:", match.group())
import re
pattern = r"<.*?>"
text = "<html><head><title>Title</title></head></html>"
match = re.search(pattern, text)
if match:
print("Non-greedy match:", match.group())
正則表達式提供了一些預定義的字符集,用于匹配特定類型的字符:
\d
:匹配任意數字字符,等價于 [0-9]
。\D
:匹配任意非數字字符,等價于 [^0-9]
。\w
:匹配任意字母、數字或下劃線字符,等價于 [a-zA-Z0-9_]
。\W
:匹配任意非字母、數字或下劃線字符,等價于 [^a-zA-Z0-9_]
。\s
:匹配任意空白字符,包括空格、制表符、換行符等。\S
:匹配任意非空白字符。import re
pattern = r"\d+"
text = "There are 3 apples and 5 oranges."
matches = re.findall(pattern, text)
print("Digits:", matches)
正則表達式還支持邊界匹配,用于匹配字符串的邊界位置:
\b
:匹配單詞邊界。\B
:匹配非單詞邊界。\A
:匹配字符串的開頭。\Z
:匹配字符串的結尾。import re
pattern = r"\bworld\b"
text = "hello world"
match = re.search(pattern, text)
if match:
print("Match found:", match.group())
Python 的 re
模塊提供了一些標志,用于修改正則表達式的匹配行為。常見的標志包括:
re.IGNORECASE
或 re.I
:忽略大小寫。re.MULTILINE
或 re.M
:多行模式,使 ^
和 $
匹配每行的開頭和結尾。re.DOTALL
或 re.S
:使 .
匹配包括換行符在內的所有字符。import re
pattern = r"hello"
text = "HELLO world"
match = re.search(pattern, text, re.IGNORECASE)
if match:
print("Match found:", match.group())
對于需要多次使用的正則表達式,可以使用 re.compile()
函數將其編譯為正則表達式對象,以提高效率。
import re
pattern = re.compile(r"\d+")
text = "There are 3 apples and 5 oranges."
matches = pattern.findall(text)
print("Digits:", matches)
正則表達式在實際開發中有廣泛的應用,以下是一些常見的應用場景:
正則表達式是處理文本的強大工具,掌握其基本語法和常用函數對于 Python 開發者來說非常重要。本文介紹了正則表達式的基本語法、常用函數、分組與捕獲、貪婪與非貪婪匹配、預定義字符集、邊界匹配、標志、編譯以及常見應用場景。希望這些知識點能夠幫助讀者更好地理解和使用 Python 正則表達式。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。