Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Regular Expression Extractor in JMeter

DZone's Guide to

Using Regular Expression Extractor in JMeter

In this tutorial, learn what the regular expression extractor is for, and how to use it with JMeter to capture dynamic values.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

The regular expression extractor is used to capture the dynamic data- from the response header or body, from the main sample or subsamples, or even from another JMeter variable- and store it into a variable for future use. It allows the user to extract values from a server response using a Perl-type regular expression. As a post-processor, this element will execute after each Sample request in its scope, applying the regular expression, extracting the requested values, generating the template string, and storing the result in the given variable name.

Capture7.PNG

How to Use Regular Expression Extractor to Capture Dynamic Values

capture8.png

If the match number is set to a non-negative number, and a match occurs, the variables are set as follows:

  • refName – the value of the template

  • refName_gn, where n=0,1,2 – the groups for the match

  • refName_g – the number of groups in the Regex (excluding 0)

If no match occurs, then the refName variable is set to the default (unless this is absent). Also, the following variables are removed:

  • refName_g0

  • refName_g1

  • refName_g

If the match number is set to a negative number, then all the possible matches in the sampler data are processed. The variables are set as follows:

  • refName_matchNr – the number of matches found; could be 0

  • refName_n, where n = 1, 2, 3 etc. – the strings as generated by the template

  • refName_n_gm, where m=0, 1, 2 – the groups for match n

  • refName – always set to the default value

  • refName_gn – not set

Note that the refName variable is always set to the default value in this case, and the associated group variables are not set.

To verify the regular expression, we can use either regular expression tester from JMeter itself, or we have a good website to validate the regular expression, www.regexpal.com.

Character classes
. any character except newline
\w \d \s word, digit, whitespace
\W \D \S not word, digit, whitespace
[abc] any of a, b, or c
[^abc] not a, b, or c
[a-g] character between a & g
Anchors
^abc$ start / end of the string
\b word boundary
Escaped characters
\. \* \\ escaped special characters
\t \n \r tab, linefeed, carriage return
\u00A9 unicode escaped ©
Groups & Lookaround
(abc) capture group
\1 backreference to group #1
(?:abc) non-capturing group
(?=abc) positive lookahead
(?!abc)

negative lookahead

Practical Exposure

If the request parameters contain multiple values as mentioned below, we have to capture all the values and subsequently pass them.

Capture9

We have to give the match number attribute as -1 to capture all the values in the reference name, as below:

Capture10

If the match number is set to a negative number, then all the possible matches in the sampler data are processed. The variables are set as follows:

  • refName_matchNr – the number of matches found; could be 0

  • refName_n, where n = 1, 2, 3 etc. – the strings as generated by the template

  • refName_n_gm, where m=0, 1, 2 – the groups for match n

  • refName – always set to the default value

  • refName_gn – not set

Note that the refName variable is always set to the default value in this case, and the associated group variables are not set.

Bookid_matchNr gives the total number of matches; in this example, it is shown as 4:

Capture11https://performanceengineeringsite.wordpress.com/2017/07/29/using-regular-expression-extractor-in-jmeter-part-2/

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
jmeter ,performance ,regex

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}