用其他一些字符替换字符串中的字符会产生异常

时间:2019-05-13 05:32:11

标签: java jmeter beanshell

我正在使用正则表达式"Ehj113GPTAw9RMbTgxce2jqh+2rn7tLWFz5JHyhxa+K68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH"

提取一些值

我想用'%2B'替换'+'并将其作为"Ehj113GPTAw9RMbTgxce2jqh%2B2rn7tLWFz5JHyhxa%2BK68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH"发送给第二个请求

我该如何实现?

我已经尝试过replaceAllreplace,但是我在某个地方出错了。

var Connect = vars.get("92_ConnectionToken").toString();
log.info(Connect);
String ConnectToken = Connect.replaceAll("+", "%2B");
String ConnectFinal = ConnectToken.replaceAll("/", "%2F");
vars.put("92_ConnectionToken", ConnectFinal);
log.info(ConnectFinal);

预期结果:

Ehj113GPTAw9RMbTgxce2jqh%2B2rn7tLWFz5JHyhxa%2BK68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH.

此字符串应作为第二个请求的输入发送。

实际结果:

2019-05-13 10:37:22,343 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: `` var Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); Str . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll
2019-05-13 10:37:22,343 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: `` var Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); Str . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll.

1 个答案:

答案 0 :(得分:1)

如果要将String特定方法用作replaceAll,请将变量定义为String

String Connect = vars.get("92_ConnectionToken");

您需要将正则表达式特殊字符转义为+

String ConnectToken = Connect.replaceAll("\\+", "%2B");

正如@kumesana所说,最好使用replace方法:

String ConnectToken2 = Connect.replace("+", "%2B");