Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向python正则表达式添加标志?_Python_Regex_Flags - Fatal编程技术网

如何向python正则表达式添加标志?

如何向python正则表达式添加标志?,python,regex,flags,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') 或者,可以使用(?iLmsux)语

我想在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')
或者,可以使用
(?iLmsux)
语法添加标志:

re.sub('(?i)test is good', 'yup', 'TEST IS GOOD')

对于
re
模块来说,是一个很好的起点。或者另一个变体:
re.sub('test is good','yup','test is good',flags=re.IGNORECASE)
您也可以使用
r.sub
,因为regex对象有一个
sub
方法。当OP说“它没有
.sub
方法”时,我不知道他在说什么。