如何在沒有 re.compile 的情況下編寫一個不區分大小寫的 Python 正則表示式?


我們可以將 re.IGNORECASE 傳遞給 search、match 或 sub 的 flags 引數 −

示例

import re
print (re.search('bush', 'BuSh', re.IGNORECASE))
print (re.match('bush', 'BuSh', re.IGNORECASE))
print (re.sub('bush', 'xxxx', 'Bushmeat', flags=re.IGNORECASE))

輸出

<_sre.SRE_Match object at 0x0000000005316648>
<_sre.SRE_Match object at 0x0000000005316648>
xxxxmeat

更新於: 03-11-2023

11K+ 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.