Skip to content

__regexFunction

JMeter __regexFunction function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values.

Difficulty
intermediate
Guide type
reference
Estimated read time
3 min read
Last verified version
Verified JMeter 5.6

Also documented in context in the full Functions and Variables reference.

The Regex Function is used to parse the previous response (or the value of a variable) using any regular expression (provided by user). The function returns the template string with variable values filled in.

The __regexFunction can also store values for future use. In the sixth parameter, you can specify a reference name. After this function executes, the same values can be retrieved at later times using the syntax for user-defined values. For instance, if you enter โ€œrefNameโ€ as the sixth parameter you will be able to use:

  • \${refName} to refer to the computed result of the second parameter (โ€œTemplate for the replacement stringโ€) parsed by this function
  • \${refName_g0} to refer to the entire match parsed by this function.
  • \${refName_g1} to refer to the first group parsed by this function.
  • \${refName_g#} to refer to the nth group parsed by this function.
  • \${refName_matchNr} to refer to the number of groups found by this function.
NameRequiredDescription
First argumentYesThe first argument is the regular expression to be applied to the response data. It will grab all matches. Any parts of this expression that you wish to use in your template string, be sure to surround in parentheses. Example: <a href="(.*)">. This will grab the value of the link and store it as the first group (there is only 1 group). Another example: <input type="hidden" name="(.*)" value="(.*)">. This will grab the name as the first group, and the value as the second group. These values can be used in your template string
Second argumentYesThis is the template string that will replace the function at run-time. To refer to a group captured in the regular expression, use the syntax: $[group_number]$. I.e.: $1$, or $2$. Your template can be any string.
Third argumentNo, default=1The third argument tells JMeter which match to use. Your regular expression might find numerous matches. You have four choices: - An integer - Tells JMeter to use that match. โ€˜1โ€™ for the first found match, โ€˜2โ€™ for the second, and so on - RAND - Tells JMeter to choose a match at random. - ALL - Tells JMeter to use all matches, and create a template string for each one and then append them all together. This option is little used. - A float number between 0 and 1 - tells JMeter to find the Xth match using the formula: (number_of_matches_found * float_number) rounded to nearest integer.
Fourth argumentNoIf โ€˜ALLโ€™ was selected for the above argument value, then this argument will be inserted between each appended copy of the template value.
Fifth argumentNoDefault value returned if no match is found
Sixth argumentNoA reference name for reusing the values parsed by this function. Stored values are \${refName} (the replacement template string) and \${refName_g#} where โ€œ#โ€ is the group number from the regular expression (โ€œ0โ€ can be used to refer to the entire match).
Seventh argumentNoInput variable name. If specified, then the value of the variable is used as the input instead of using the previous sample result.
On this page