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)