有没有办法使用由动态变量(例如 ${var})组成的正则表达式来设置正则表达式提取器。
提出这个问题的理由是因为我的测试计划的一部分是从html响应中获取某个用户帐户的用户ID,因此随后Jmeter将继续以该用户ID作为参考来开展其业务。如果我只担心测试计划的 1 个线程,它将如下所示简单
<.*id=(/d+).*value="johndoe"
但我希望测试计划足够灵活以处理多个线程,每个线程代表一个唯一的用户,因此正则表达式必须是某种东西如下所示,
<.*id=(/d+).*value="${USERNAME}"
我们将不胜感激关于如何实现这一目标的一两个建议。如果无法实现,其他方法也很好,
谢谢
Is there any way to set up a regex extractor with a regex that consists of dynamic variable (e.g, ${var}).
The rationale asking is because one section of my test plan is to get the User ID of a certain user account from the html response, so subsequently Jmeter will continue doing its business with that User ID as the reference. If I only worry about 1 thread to the test plan, it will be as simple as below
<.*id=(/d+).*value="johndoe"
But I want the test plan to be flexible enough to handle multiple thread with each thread represents a unique user, so the regular expression will have to be something like below
<.*id=(/d+).*value="${USERNAME}"
One or two of advices on how to achieve this will be appreciated. If it's not achievable, an alternative way will also be good
Thanks
发布评论
评论(8)
您好,
作为一名即将成为 Jmeter 爱好者的人,您会发现这种情况经常发生。您只需转义正则表达式中的特殊字符即可。美元符号在 PERL 正则表达式中具有特殊含义,因此我们需要告诉正则表达式使用文字 $:
此外,id 部分有点贪婪。我会推荐:
Greetings,
As a soon-to-be Jmeter lover, you'll find this happens often. You simply need to escape the special characters in your regex. The dollar sign has special meaning in PERL regular expressions, so we need to tell the regex to use a literal $:
Also, the id section is a bit greedy. I would recommend:
我遇到了类似的问题,简单地转义 $ 字符对我来说不起作用。
相反,我必须使用 __V() 函数。
因此,在此示例中,正则表达式如下所示:
我不必转义 $ 字符。
我正在使用 JMeter 2.9
I had a similar problem and simply escaping the $ character didn't work for me.
Instead I had to use the __V() function.
So in this example, the regular expression would be like below:
I didn't have to escape the $ character.
I'm using JMeter 2.9
在 Apache JMeter 2.9 中,在正则表达式中包含变量值 ${MYVARIABLE} 的正确表达式是:
这应该放置在正则表达式中所需的位置。
工作正常!
In Apache JMeter 2.9, the correct expression the include a variable value ${MYVARIABLE} inside a Regular Expression is:
This should be placed in the desired place in the regex.
Works fine!!!
对我来说,不需要转义任何东西(在 Jmeter 变量语法中)。我正在使用 JMeter 2.9
仅设置变量(包含整个正则表达式,可能是动态生成的),例如“正则表达式”字段中的 ${regex} 也有效。
For me there is no need to escape anything (in Jmeter variables syntax). I'm using JMeter 2.9
Setting only variable (containing the whole regex, maybe dynamically generated) like ${regex} in the field "Regular Expression" also works.
这个问题也让我很恼火。
对我有用的唯一解决方案是添加一个带有此类代码的 BeanShell Sampler:
String S = "blah-blah-blah...\\\d...${My变量}...blah-blah-blah";
vars.put("RegExp", S);
然后在正则表达式提取器中仅使用this:
${RegExp}
注意,由于某种原因,\d 需要用 三个 反斜杠进行筛选...facepalm.jpg
This issue was irritating me too.
The only solution that worked for me was adding a BeanShell Sampler with code of this sort:
String S = "blah-blah-blah...\\\d...${My variable}...blah-blah-blah";
vars.put("RegExp", S);
And Then in Regular Expression Extractor a used only this:
${RegExp}
NB Note that for some reason \d was needed to be screened with THREE backslashes...facepalm.jpg
对于 Jmeter 3.1,我必须使用 ${__V(${MYVAR})} 语法,如@spark和@carlos AG所述
For Jmeter 3.1 I had to use the ${__V(${MYVAR})} syntax as @spark and @carlos AG stated
我这样做的唯一方法是,首先创建一个字符串,连接变量和模式的其余部分并将其保存到变量中(在后处理器中)。并将其放入正则表达式提取器中。
The only way I did this was, First creating a String, concatenating the variables and the rest of the pattern and saving this into a variable (this in a PostProcessor). And putting it in the regular expression extractor.
我在 jMeter 5.5-Snapshot 3a74a92 上描述了非常类似的问题,并提供了工作解决方案: jMeter 正则表达式提取器在正则表达式中使用变量
I described very similar problem on jMeter 5.5-Snapshot 3a74a92 with working solution here: jMeter Regular Expression Extractor with use of variable in regexp