Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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_Python 3.x - Fatal编程技术网

如何在python中处于详细模式的正则表达式对象中使用变量?

如何在python中处于详细模式的正则表达式对象中使用变量?,python,python-3.x,Python,Python 3.x,我目前已设置了以下正则表达式对象(我已将其编辑为稍微精简一点) myRegex=re.compile(r''+re.escape(inputCharacter)+r'') inputCharacter指用户先前输入的字符。 为了我自己的利益,我通常更喜欢在正则表达式中使用verbose模式,但是,经过研究,我仍然不确定如何在verbose正则表达式对象中使用变量(如果可以的话) 例如: myRegex=re.compile(r'' MYVARIABLE '',重复冗长) 当我在多行正则表达式

我目前已设置了以下正则表达式对象(我已将其编辑为稍微精简一点)

myRegex=re.compile(r''+re.escape(inputCharacter)+r'')
inputCharacter
指用户先前输入的字符。 为了我自己的利益,我通常更喜欢在正则表达式中使用verbose模式,但是,经过研究,我仍然不确定如何在verbose正则表达式对象中使用变量(如果可以的话)

例如:

myRegex=re.compile(r''
MYVARIABLE
'',重复冗长)
当我在多行正则表达式中使用re.escape时,它将被视为要匹配的项


谢谢

我个人更喜欢带有这种注释的f字串:

regex=(
#评论
r“\.[^.]*”
#更多评论
rf“{variable1}[]”
)

并且忘记re.VERBOSE-flag,因为它使调试器难以使用。我想是个人偏好。

可能
myRegex=re.compile(r''{}'''.format(MYVARIABLE),re.VERBOSE)