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

Python 为什么我的“搜索”命令不起作用?

Python 为什么我的“搜索”命令不起作用?,python,regex,wlst,Python,Regex,Wlst,我的脚本中有以下“如果”条件: if ( re.search(argVariable, newArgs) ): 但是,当我为“argVariable”传递某个值时,它失败了 样本输出: Searching for the argument: -XX:+HeapDumpOnOutOfMemoryError argVariable: "-XX:+HeapDumpOnOutOfMemoryError" "-XX:+HeapDumpOnOutOfMemoryError" is MISSING, Add

我的脚本中有以下“如果”条件:

if ( re.search(argVariable, newArgs) ):
但是,当我为“argVariable”传递某个值时,它失败了

样本输出:

Searching for the argument: -XX:+HeapDumpOnOutOfMemoryError
argVariable: "-XX:+HeapDumpOnOutOfMemoryError"
"-XX:+HeapDumpOnOutOfMemoryError" is MISSING, Adding...
我确信我的'newArgs'变量已经有了它。(见下文)


我做错了什么?

我想我需要在使用它之前重新转义实际值。然后我需要使用编译版本,以避免变量中的特殊字符被解释为正则表达式的特殊字符:

compiledArgVariable = re.compile(re.escape(argVariable))
if ( re.search(compiledArgVariable, newArgs) ):
示例输出显示它找到了确切的值:

argVariable: "-XX:+HeapDumpOnOutOfMemoryError"
"-XX:+HeapDumpOnOutOfMemoryError" found to be an EXISTING argument. Checking if it has the correct value...
"-XX:+HeapDumpOnOutOfMemoryError" HAS the correct value. Skipping..

您需要退出正则表达式,请使用:

argVariable: "-XX:+HeapDumpOnOutOfMemoryError"
"-XX:+HeapDumpOnOutOfMemoryError" found to be an EXISTING argument. Checking if it has the correct value...
"-XX:+HeapDumpOnOutOfMemoryError" HAS the correct value. Skipping..
escapedRegex = re.escape(regex)