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


可以將 re.IGNORECASE 傳遞到 search、match 或 sub 的標誌引數 -

示例

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

更新日期: 2023-11-03

11,000+ 瀏覽次數

啟動您的 職業

透過完成本課程獲得認證

開始
廣告
© . All rights reserved.