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

时间:2022-06-01 20:10:40

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\",\"\",\"

3 个解决方案

#1


1  

Use super secret operator (Negative match N)如何在Jmeter中使用正则表达式提取多个值

使用超级密码运算符(负匹配N)

UPD: G2 - is in my example, as I extract two groups from each encounter. each encounter is "uuid" in g1 and g2 is second part I need second part here. that's why $2$ template and g2. If your encounters in one group you ll most likely use $1$ template that will place all encounters into g1. If you have one match group you don't actually need _gN ending at all. To understand more the variables after group extraction add a "Debug PostProcessor" and inspect output in TreeView.

UPD:G2 - 在我的例子中,因为我从每次遭遇中提取两组。每次遭遇都是g1中的“uuid”,g2是第二部分,我需要第二部分。这就是$ 2 $模板和g2的原因。如果你遇到一个组,你很可能会使用$ 1 $模板将所有遭遇放入g1。如果你有一个匹配组,你根本不需要_gN结束。要在组提取后了解更多变量,请添加“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. In most cases it's next you do after extraction.

很好的两个知道像“For each”这样的控制元素可以理解组,并且可以使用regexUUID_这样的前缀并逐步完成。在大多数情况下,接下来你会在提取后做。

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

UPD2。有问题的regexp的原始版本(insertar \?sIws2kyXGJJA_ \ d *)==([^ [] *)与模板$ 1 $$ 2 $你将拥有g1组中的第一部分和g2中的第二部分

#2


3  

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). Though it is also correct, suggesting better approach to achieve the same.

在DMC给出的答案中,您需要添加正则表达式提取器TWICE以匹配/检索具有不同匹配号(1,2)的两个值。虽然它也是正确的,但建议采用更好的方法来实现同样的目标。

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.

您可以使用Template同时捕获这两个值,然后使用索引来引用它。

Please check the following screen shot:

请检查以下屏幕截图:

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

Here, we captured both the values using two groups into two different templates, as $1$ and $2$ respectively. 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$.)

在这里,我们使用两个组将这两个值捕获到两个不同的模板中,分别为$ 1 $和$ 2 $。这里,模板默认按照正则表达式中指定的组的顺序存储数据。 (仅供参考,您也可以通过调整模板的顺序来更改订单,例如$ 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.

注意:使用模板,您可以使用多个组检索任意数量的值,并使用单个正则表达式提取器通过索引来引用它们。

#3


2  

I'm sure there is a more efficient way but this worked:

我确信有一种更有效的方法,但这有效:

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

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

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中,如果您传递其中一个值:

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

The Matching No should be 1 for the first, and 2 for the second match i believe.

匹配No应该是1为第一,2为第二场比赛我相信。

#1


1  

Use super secret operator (Negative match N)如何在Jmeter中使用正则表达式提取多个值

使用超级密码运算符(负匹配N)

UPD: G2 - is in my example, as I extract two groups from each encounter. each encounter is "uuid" in g1 and g2 is second part I need second part here. that's why $2$ template and g2. If your encounters in one group you ll most likely use $1$ template that will place all encounters into g1. If you have one match group you don't actually need _gN ending at all. To understand more the variables after group extraction add a "Debug PostProcessor" and inspect output in TreeView.

UPD:G2 - 在我的例子中,因为我从每次遭遇中提取两组。每次遭遇都是g1中的“uuid”,g2是第二部分,我需要第二部分。这就是$ 2 $模板和g2的原因。如果你遇到一个组,你很可能会使用$ 1 $模板将所有遭遇放入g1。如果你有一个匹配组,你根本不需要_gN结束。要在组提取后了解更多变量,请添加“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. In most cases it's next you do after extraction.

很好的两个知道像“For each”这样的控制元素可以理解组,并且可以使用regexUUID_这样的前缀并逐步完成。在大多数情况下,接下来你会在提取后做。

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

UPD2。有问题的regexp的原始版本(insertar \?sIws2kyXGJJA_ \ d *)==([^ [] *)与模板$ 1 $$ 2 $你将拥有g1组中的第一部分和g2中的第二部分

#2


3  

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). Though it is also correct, suggesting better approach to achieve the same.

在DMC给出的答案中,您需要添加正则表达式提取器TWICE以匹配/检索具有不同匹配号(1,2)的两个值。虽然它也是正确的,但建议采用更好的方法来实现同样的目标。

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.

您可以使用Template同时捕获这两个值,然后使用索引来引用它。

Please check the following screen shot:

请检查以下屏幕截图:

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

Here, we captured both the values using two groups into two different templates, as $1$ and $2$ respectively. 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$.)

在这里,我们使用两个组将这两个值捕获到两个不同的模板中,分别为$ 1 $和$ 2 $。这里,模板默认按照正则表达式中指定的组的顺序存储数据。 (仅供参考,您也可以通过调整模板的顺序来更改订单,例如$ 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.

注意:使用模板,您可以使用多个组检索任意数量的值,并使用单个正则表达式提取器通过索引来引用它们。

#3


2  

I'm sure there is a more efficient way but this worked:

我确信有一种更有效的方法,但这有效:

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

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

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中,如果您传递其中一个值:

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

The Matching No should be 1 for the first, and 2 for the second match i believe.

匹配No应该是1为第一,2为第二场比赛我相信。