[英]how add flags to python regex?
我想在我的re.sub regex字符串中添加一個標志。 在PHP中,我會這樣做' \\test is good\\i
'
我在re.compile中嘗試過此方法,但是它沒有.sub
方法。 我試圖使用s.replace
,但我也不能將i
標志添加到此
編譯的正則表達式對象可以傳遞給re.sub()
,因此仍然可以在編譯時傳遞標志。
r = re.compile('test is good', re.IGNORECASE)
re.sub(r, 'yup', 'TEST IS GOOD')
或者,可以使用(?iLmsux)
語法添加標志:
re.sub('(?i)test is good', 'yup', 'TEST IS GOOD')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.