Sebastian
Sebastian

Reputation: 1783

In JMeter how can I capture multiple (varying) values of a regular expression?

I need to parse a html page that can contain multiple (unspecified how many) links. Here an example (pseudo html):

<a href="../entrygroups/GROUPIDENTIFIER/3">edit</a>
[...]
<a href="../entrygroups/GROUPIDENTIFIER/7">edit</a>
[...]
<a href="../entrygroups/GROUPIDENTIFIER/12">edit</a>
[...]
<a href="../entrygroups/GROUPIDENTIFIER/16">edit</a>

I am basically just interested in the numbers 3,7,12,16 of the url.

Is there a way to put those values into some sort of array and loop over them (in a for each kind of way).

Is this possible and if so, how would I do this? I looked at the regular expression extractor but it seems that that one can only assign a fixed set of groups to a fixed set of variables.

Upvotes: 1

Views: 5063

Answers (2)

Dmitri T
Dmitri T

Reputation: 168092

  1. Extract the values using Regular Expression Extractor configured like:

    • Reference Name: anything meaningful, i.e. edit
    • Regular Expression: <a href="../entrygroups/GROUPIDENTIFIER/(.+?)">edit</a>
    • Template: $1
    • Match No: -1

      JMeter Regular Expression Extractor

  2. Add ForEach Controller configured like:

    • Input variable prefix: edit (or whatever you used as the Regular Expression Extractor Reference Name)
    • Output variable prefix: again anything meaningful, i.e. current_edit

      enter image description here

  3. Put the sampler(s) you need under the ForEach Controller
  4. Refer the "GROUPIDENTIFIER" value as ${current_edit} where required

    JMeter Regular Expression Extractor Looping Values

See Using Regular Expressions in JMeter guide for another example of looping through all links found in the page with the Regular Expression Extractor and ForEach Controller.

Upvotes: 4

timbre timbre
timbre timbre

Reputation: 13980

Actually RegEx extractor is able to do exactly what you want, if you specify Match No. field with less than 0, e.g. -1:

enter image description here

As a result, you will get N variables (using your input as example):

id_1=3
id_2=7
id_3=12
id_4=16

There also will be variable which has a count of matches:

id_matchNr=4

Upvotes: 2

Related Questions