Why and How to use the Regular Expression Extractor (post-processors) in JMeter

Priyanka Gupta
Tradeling Technology Blog

--

Regular Expression Extractor is useful for extracting information from the response, using a single regular expression you can extract multiple dynamic values provided that the regular expression should correct.

  • Run an HTTP request to get a response:
image 1.1
  • The user gets the token in the API response. Now, if the user needs the same token to process some other API request-

How to add and use the JMeter Regular Expression Extractor-

  1. Regular Expression Extractor is a post-processor that can be used to apply regex on response data. The matched expression derived by applying the regex can then be used in a different sampler dynamically in the test plan execution.
  2. To add a Regex Extractor- Select the HTTP Request Sampler (Manage), and right-click Add → Post Processor → Regular Expression Extractor. It is showing under the post-processor which can be used after the request is processed.

Add Regular Expression Extractor to extract the specific data from the response:

image 1.2

3. Regular Expression Exactor page is to capture the response of the call, and will explain the fields available in the below image:

image 1.3

a) “Apply to”- You can choose whether the regular expression will be applied to which field, please refer to the attached images 1.3 and 1.6 for the fields

b) JMeter Variable Name to use: the expression is applied to the filled-in variable

c) “Field to check”- You can choose which field the regular expression is applied to, can refer to attached images 1.3 and 1.6 for the available fields

d) “Name of created variable”- Give a variable name to store the response variable of the request call and this value can be used in any call with the syntax: ${variable_name}

e) Regular Expression- Fill in the regular expression of the particular attribute

f) Template ($i$, where i is capturing the group number, starts at 1)- Choose the group you want to extract from the regular expression, ‘$1$’ will extract group 1, ‘$2$’ will extract group 2, in a similar way we can extract any group but $0$ will extract the entire expression.

g) Match No. (0 for Random)- If multiple groups are found for the matching value then we need to specify the matching group we want to fetch (If you give 1, it will fetch you the 1 result found) and 0 is for a random group.

h) Default- Can give any default value or can check the checkbox “Use empty default value”- it will give the empty value.

4. Fetch the token from image 1.1 using the Regex Extractor. Find the regex for the token: `{“token”:”([^”]+)”}`. Please check the below images, as these images will show you how to find the matches looks like:

image 1.5
image 1.6

We are doing the below two actions:

  1. Locating the Token in Your JMeter Script
  2. Extracting the Token with the JMeter Regular Expression Extractor

With this flow, we can successfully grab the token into the given variable:

image 1.7

Now pass the Regex Extractor variable to any of the HTTP calls with the syntax: ${login_token}, Please check the below screen for the reference:

image 1.8

Add view Result Tree to check the request/response headers and body of the API, please refer to the below image:

image 1.9

Check the response, API request headers are containing the token:

image 1.9.1

The below links can help you to locate the exact regular expression:

Go through the https://www.geeksforgeeks.org/write-regular-expressions/ if you want to learn more about regular expressions.

Thank you for reading the blog, Keep learning :D

--

--

Priyanka Gupta
Tradeling Technology Blog

Over 8 years of experience as a SDET in Web, Mobile and API automation, Cypress, JS, Selenium, Java, BDD, Appium, Postman, Jmeter