我想在Jemeter中使用beanshell预处理器执行正则表达式

时间:2015-07-14 12:38:23

标签: regex jmeter beanshell

这是我写的代码,

import java.util.regex.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

String pStr ="Review Required";

int count = 50

for(int i=0;i<count;i++) 
{
String tmp = "wireReviewApprove:headerTable:0:wireStatusValue">\(\.\+\?\)<\/span>";
Pattern patternN = Pattern.compile(tmp);
Matcher matcher = patternN.matcher(pStr);

if (matcher.find())
    {
sampler.addArgument("selectedItemId["+i+"]","true");
    }
}

给出错误:ERROR - jmeter.util.BeanShellInterpreter:调用bsh方法时出错:eval源文件:内联评估:import java.util.regex.*; import java.util.regex.Matcher; import java.util.reg . . . '' Token Parsing Error: Lexical error at line 14, column 64. Encountered: "\\" (92), after : "" 2015/07/14 08:15:15 WARN - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: import java.util.regex。*; import java.util.regex.Matcher; import java.util.reg。 。 。 &#39;&#39;令牌解析错误:第14行第64列的词汇错误。遇到:&#34; \&#34; (92),之后:&#34;&#34;

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

  1. int count = 50
  2. 后缺少分号
  3. 转义tmp字符串的问题。如果原件看起来像:

    wireReviewApprove:headerTable:0:wireStatusValue">\(\.\+\?\)<\/span>
    

    你需要看起来像:

    String tmp = "wireReviewApprove:headerTable:0:wireStatusValue\">\\(\\.\\+\\?\\)<\\/span>";
    
  4. Full Beanshell代码:

    int count = 50;
    
    for (int i = 0; i < count; i++) {
        String tmp = "wireReviewApprove:headerTable:0:wireStatusValue\">\\(\\.\\+\\?\\)<\\/span>";        
        Pattern patternN = Pattern.compile(tmp);
        Matcher matcher = patternN.matcher(pStr);
    
        if (matcher.find()) {
            sampler.addArgument("selectedItemId[" + i + "]", "true");
        }
    }
    

    有关JMeter中Beanshell脚本的更多信息,请参阅How to use BeanShell: JMeter's favorite built-in component指南。

    N.B。最好切换到JSR223 Sampler和Groovy语言,就像大负载一样,Beanshell可能成为瓶颈。