无法替换包含特殊字符“[”的字符串

时间:2021-04-05 15:20:09

标签: groovy jmeter replaceall

我的常规版本是 3.0.7。

我试过了:

String str1="Hello[World[Test"
String str2=str1.replaceAll("\\[","")

错误详情:

2021-04-06 01:11:33,087 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script106.groovy: 19: Unexpected input: '(' @ line 19, column 24.
String str2 = str1.replaceAll("\[","")

我怎样才能做到这一点?

String str2=str1.replaceAll("\\[","")

2 个答案:

答案 0 :(得分:1)

您可以使用 replaceAll 提供的 String 方法。

groovy:000> str1="Hello[World[Test"
===> Hello[World[Test
groovy:000> str1.replaceAll('\\[', '')
===> HelloWorldTest
~ $ groovy -version
Groovy Version: 2.5.13 JVM: 1.8.0_202 Vendor: Azul Systems, Inc. OS: Mac OS X
~ $ 
~ $ cat doit.groovy 
String str1='Hello[World[Test'
String str2 = str1.replaceAll('\\[', '')
println str2
~ $ 
~ $ groovy doit
HelloWorldTest

答案 1 :(得分:0)

我无法使用 enter image description here

附带的最新 JMeter 5.4.1 重现您的问题

Groovy 3.0.7

问题不在于这两行,而是在其他地方,就在这些行之前或之后,如果没有看到完整的后处理器脚本,很难说到底在哪里。

此外,如果这是您的代码唯一要做的事情,您可能需要考虑使用 enter image description here__strReplace(),它们是 __strReplaceRegex(),可以使用 Custom JMeter Functions 安装

JMeter Plugins Manager