Pet*_*man 2 python regex flags
我想在re.sub正则表达式字符串中添加一个标志.在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')
Run Code Online (Sandbox Code Playgroud)
或者,可以使用(?iLmsux)语法添加标志:
re.sub('(?i)test is good', 'yup', 'TEST IS GOOD')
Run Code Online (Sandbox Code Playgroud)