JMeter 中配备动态正则表达式的 Regex Extractor

发布于 2024-10-24 16:24:20 字数 402 浏览 2 评论 0 原文

有没有办法使用由动态变量(例如 ${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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一个人的旅程 2024-10-31 16:24:20

您好,

作为一名即将成为 Jmeter 爱好者的人,您会发现这种情况经常发生。您只需转义正则表达式中的特殊字符即可。美元符号在 PERL 正则表达式中具有特殊含义,因此我们需要告诉正则表达式使用文字 $:

<.*id=(/d+).*value="\${USERNAME}"

此外,id 部分有点贪婪。我会推荐:

<.*id=(/d+?) value="${USERNAME}"

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 $:

<.*id=(/d+).*value="\${USERNAME}"

Also, the id section is a bit greedy. I would recommend:

<.*id=(/d+?) value="${USERNAME}"
岁月无声 2024-10-31 16:24:20

我遇到了类似的问题,简单地转义 $ 字符对我来说不起作用。

相反,我必须使用 __V() 函数。
因此,在此示例中,正则表达式如下所示:

<.*id=(/d+).*value="${__V(${USERNAME})}"

我不必转义 $ 字符。
我正在使用 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:

<.*id=(/d+).*value="${__V(${USERNAME})}"

I didn't have to escape the $ character.
I'm using JMeter 2.9

眼前雾蒙蒙 2024-10-31 16:24:20

在 Apache JMeter 2.9 中,在正则表达式中包含变量值 ${MYVARIABLE} 的正确表达式是:

__V(${MYVARIABLE})

这应该放置在正则表达式中所需的位置。

工作正常!

In Apache JMeter 2.9, the correct expression the include a variable value ${MYVARIABLE} inside a Regular Expression is:

__V(${MYVARIABLE})

This should be placed in the desired place in the regex.

Works fine!!!

不再见 2024-10-31 16:24:20

对我来说,不需要转义任何东西(在 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.

同展鸳鸯锦 2024-10-31 16:24:20

这个问题也让我很恼火。
对我有用的唯一解决方案是添加一个带有此类代码的 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

獨角戲 2024-10-31 16:24:20

对于 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

玩心态 2024-10-31 16:24:20

我这样做的唯一方法是,首先创建一个字符串,连接变量和模式的其余部分并将其保存到变量中(在后处理器中)。并将其放入正则表达式提取器中。

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.

恋竹姑娘 2024-10-31 16:24:20

我在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文