在日常工作中,常常要對文字做處理,例如搜尋、驗證使用者輸入、擷取並重組文字等,遇到與字串有關的問題使用正規表達式可以免去很多處理上的麻煩,讓程式碼更簡單好懂。
雖然只用單純的字串方法,像是split()、replace()、find()也是可以做到,但當遇到需要『規則化』的需求時,會讓程式碼變的冗長,這時候『正規表達式』好用的地方就出現了!
先來説説何謂『正規表達式』,它是一種模式語言 Pattern language,專門用來做匹配字串的,可以想像成它是『文字版的搜尋公式』,當定義好之後就能利用它快速找到符合的條件。
想要在 Python 中使用正規表達式,我們需要先引入re模組,這樣就可以使用它來確認或尋找特定的模式。
import re
主要可以分成以下幾大類:
.:任意字元,換行除外\d:數字0~9,等同於[0-9]\D:非數字\w:數字、字母、下底線,等同於[0-9A-Za-z_]\W:非數字、字母、下底線\s:空白(Tab、空格、換行等)\S:非空白*:0次或多次+:1次或多次?:0次或1次{n}:剛好n次{n,}:至少n次{n,m}:介於n至m之間import re
print(re.findall(r"\d+", "我是小明,今年18歲。")) # ["18"]
^:字串開頭$:字串結尾\b:單字邊界(空白或符號分割地方)\B:非單字邊界print(re.findall(r"^Hello", "Hello World"))   # ['Hello']
print(re.findall(r"World$", "Hello World"))   # ['World']
():群組,可當作一個單位處理或是捕捉子字串|:或(OR)(?:...):非捕捉群組(只用來分組並不會儲存結果)print(re.findall(r"(dog|cat)", "I love dog and cat")) # ['dog', 'cat']
[]:一組字符集\:跳脫字元print(re.findall(r"[aeiou]", "hello world"))  # ['e', 'o', 'o']
re模組中的方法re.match():當字串為開頭時才會進行匹配,否則回傳None。import re
text = "Hello World"
m1 = re.match(r"Hello", text)   # 符合,因為字串開頭就是 "Hello"
m2 = re.match(r"World", text)   # 不符合,因為 "World" 不在字串開頭
re.search():在整個字串中搜尋,找到第一個符合條件的結果會立即停止,否則回傳None。import re
text = "I love Python and JavaScript"
s1 = re.search(r"Python", text)
s2 = re.search(r"Ruby", text)
print(s1.group())  # Python
print(s2)          # None
re.findall():回傳所有符合的結果,會以串列 List方式呈現。import re
text = "電話:0912-345-678,公司分機:02-1234-5678"
numbers = re.findall(r"\d+", text)
print(numbers) # ['0912', '345', '678', '02', '1234', '5678']
re.finditer():和findall()方法很類似,但回傳迭代器每個結果都是一個物件,可以取得更詳細的內容。import re
text = "2025-10-10  2024-12-31"
matches = re.finditer(r"\d{4}-\d{2}-\d{2}", text)
for m in matches:
    print(f"日期:{m.group()},位置:{m.span()}")
# 日期:2025-10-10,位置:(0, 10)
# 日期:2024-12-31,位置:(12, 22)
re.sub():替換符合的字串。text = "I like cats. Cats are cute!"
result = re.sub(r"cats?", "dogs", text, flags=re.IGNORECASE)
print(result) # I like dogs. dogs are cute!
re.split():切割字串。text = "apple, mango; tomato|grape"
fruits = re.split(r"[,;|]\s*", text)
print(fruits)
# ['apple', 'mango', 'tomato', 'grape']
其他的用法可以到regex101了解更多內容。
那麼今天就介紹到這,明天見ㄅㄅ!