Python 正則表示式
RegEx 模組
Python 有一個名為 re
的內建包,可用於處理正則表示式。
匯入 re
模組
import re
Python 中的正則表示式
匯入 re
模組後,即可開始使用正則表示式
示例
搜尋字串,檢視它是否以“The”開頭並以“Spain”結尾
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
自己動手試一試 »
Python 有一個名為 re
的內建包,可用於處理正則表示式。
匯入 re
模組
import re
匯入 re
模組後,即可開始使用正則表示式
搜尋字串,檢視它是否以“The”開頭並以“Spain”結尾
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
自己動手試一試 »
如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com
如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com