在 JSR223 中最后一个双斜杠后获取字符串

Get string after last double slashes in JSR223

我在 JMeter 中有一个 JSR223 Sampler 来获取最后一个 // 之后的字符串。 currentFile 命名字符串包含 JMeter 变量的名称,其中包含文件路径。

String filen = vars.get(${currentFile});
filen=filen.replaceFirst(".*//(\w+)","");

我收到错误消息:

Response message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script80.groovy: 8: unexpected char: '\' @ line 8, column 36. filen=filen.replaceFirst(".*//(\w+)",""); ^

根据JSR223 Sampler Documentation

JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.

props.get("START.HMS");
props.put("PROP1","1234");

所以你需要替换这一行:

String filen = vars.get(${currentFile});

这个:

String filen = vars.get('currentFile');

查看 Apache Groovy - Why and How You Should Use It 文章,了解有关 JMeter 测试中 Groovy 脚本的更多信息。

为什么不为此使用 Groovy。它有一个很好的运算符:

String filen = vars.get("currentFile");
def result = filen =~ /.*\/\/(.*)/; //matches end of the string after the last //
if (result.hasGroup()) {
    filen = result[0][1]
    log.info("file:"+filen)
}