简体   繁体   English

如何在Jmeter中使用正则表达式提取多个值

[英]How to extract multiple values with a regular expression in Jmeter

I am running tests with jmeter and I need to extract with a Regular Expression: 我用jmeter运行测试,我需要用正则表达式提取:

insertar?sIws2kyXGJJA_01== 
insertar?sIws2kyXGJJA_02==

in the following String: 在以下字符串中:

[\"EMBPAGE1_00010001\",\"**insertar?sIws2kyXGJJA_01==**\",1,100,\"%\",300,\"px\",0,\"center\",\"\",\"[\"EMBPAGE1_00010002\",\"**insertar?sIws2kyXGJJA_02==**\",1,100,\"%\",300,\"px\",0,\"center\",\"\",\"

In answer given by DMC, you need to add regular expression extractor TWICE to match/retrieve both the values with different Match No. (1, 2). 在DMC给出的答案中,您需要添加正则表达式提取器TWICE以匹配/检索具有不同匹配号(1,2)的两个值。 Though it is also correct, suggesting better approach to achieve the same. 虽然它也是正确的,但建议采用更好的方法来实现同样的目标。

Another Approach: 另一种方法:

1. Capture Both Values: 1.捕获两个值:

You can use Template to capture both the values at the same time , and later, refer it using indexing. 您可以使用模板在同一时间捕捉到两个值,后来,使用索引参考它。

Please check the following screen shot: 请检查以下屏幕截图:

在此输入图像描述

Here, we captured both the values using two groups into two different templates, as $1$ and $2$ respectively. 在这里,我们使用两个组将两个值捕获到两个不同的模板中,分别为$ 1 $和$ 2 $。 Here, templates store the data in the order of the groups specified in regular expression by default. 这里,模板默认按照正则表达式中指定的组的顺序存储数据。 (FYI, you can change the order also by tweaking the order of templates like $2$ and then $1$.) (仅供参考,您也可以通过调整模板的顺序来更改订单,例如$ 2 $和$ 1 $。)

Now, as in the diagram, we are capturing two values and storing them using templates: $1$ (refers to first group match) and $2$ (refers to second group match) 现在,如图所示,我们捕获两个值并使用模板存储它们:$ 1 $(指第一组匹配)和$ 2 $(指第二组匹配)

2. Retrieve Values: 2.检索值:

Now, refer these values in your script by using the following syntax: 现在,使用以下语法在脚本中引用这些值:

${insert_values_gn} (n refers to match no.) $ {insert_values_gn}(n表示匹配编号)

eg: 例如:

${insert_values_g1} - refers to the first match $ {insert_values_g1} - 指第一场比赛

${insert_values_g2} - refers to the second match $ {insert_values_g2} - 指第二场比赛

To make it simple, You can think "insert_values" as list of strings captured using multiple groups and use 'n' (1,2,3 etc) as the index to retrieve the values. 为简单起见,您可以将“insert_values”视为使用多个组捕获的字符串列表, 使用“n”(1,2,3等)作为索引来检索值。

Note: using templates, you can have any number of values can be retrieved using multiple groups and refer to them by indexing, using a single regular expression extractor. 注意:使用模板,您可以使用多个组检索任意数量的值,并使用单个正则表达式提取器通过索引来引用它们。

I'm sure there is a more efficient way but this worked: 我确信有一种更有效的方法,但这有效:

\*\*(.*?)\*\*.*\"\*\*(.*?)\*\*

在此输入图像描述

You can also use only \\*\\*(.*?)\\*\\* It will match both of them anyway, so make sure you set the right 'Matching No.' 您也可以只使用\\*\\*(.*?)\\*\\*无论如何都会匹配它们,所以请确保设置正确的“匹配编号” in Jmeter if you pass one of the values: 在Jmeter中,如果您传递其中一个值:

在此输入图像描述

The Matching No should be 1 for the first, and 2 for the second match i believe. 匹配No应该是1为第一个,2为第二个匹配我相信。

Use super secret operator (Negative match N) 使用超级密码运算符(负匹配N) 在此输入图像描述

UPD: G2 - is in my example, as I extract two groups from each encounter. UPD:G2 - 在我的例子中,因为我从每次遭遇中提取两组。 each encounter is "uuid" in g1 and g2 is second part I need second part here. 每次遭遇都是g1中的“uuid”,g2是第二部分,我需要第二部分。 that's why $2$ template and g2. 这就是$ 2 $模板和g2的原因。 If your encounters in one group you ll most likely use $1$ template that will place all encounters into g1. 如果你遇到一个组,你很可能会使用$ 1 $模板将所有遭遇放入g1。 If you have one match group you don't actually need _gN ending at all. 如果你有一个匹配组,你根本不需要_gN结束。 To understand more the variables after group extraction add a "Debug PostProcessor" and inspect output in TreeView. 要在组提取后了解更多变量,请添加“Debug PostProcessor”并在TreeView中检查输出。

It nice two know that control elements like "For each" understand groups and can work with prefix like regexUUID_ and walk through. 很好的两个知道像“For each”这样的控制元素可以理解组,并且可以使用regexUUID_这样的前缀并遍历。 In most cases it's next you do after extraction. 在大多数情况下,接下来你会在提取后做。

UPD2. UPD2。 primitive version of regexp in question (insertar\\?sIws2kyXGJJA_\\d*)==([^[]*) with template $1$$2$ you ll have the first parts in g1 group and the second parts in g2 有问题的regexp的原始版本(insertar \\?sIws2kyXGJJA_ \\ d *)==([^ [] *)与模板$ 1 $$ 2 $你将拥有g1组中的第一部分和g2中的第二部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM