The Ultimate Guide To Write Regex In Apache JMeter

 In Business, Code Quality

At my workplace, I was asked to create a JMeter script for load testing of a product. I went through the JMeter document and recorded the script. I found it easy as we just need to record and play. But hey, wait! What’s this? Script failed!! It says “401 Unauthorized”. I debugged and found that site uses Cross-Site Request Forger (CSRF) to prevent malicious attacks. I was searching for a solution and found this interesting thing that JMeter has – Regex (Regular Expression). But again, the question was “How to write the regex that extract the exact value that I needed?.”

I found some easiest way to write regex that I would like to share with you. Below are the main 4 regex that we will use often in JMeter.

  • Extract single string from single line. 
    name=”file” value=”jmeter.txt”
     – name=”file” value=”(.+?)”
     – name=”file” value=”(.+?)”

  • Extract multiple values from single line.
     – name=”(.+?)” value=”(.+?)”
  • Extract values from lines having new line, spaces, or tabs.
             value= “jmeter.txt”  
     – name=” file “\s*value=”(.*?)”

  • If want to find a only 2 values from the known list of options use the following:
     – categoryId=FISH|REPTILES” 

Over and above these there are few more metacharacters used in regular expressions that you may give a try.

\dAny digit, short for [0-9]
\wA word character, short for [a-zA-Z_0-9]


Regular ExpressionDescriptionExamples
.Matches any characters.X=… it will extract the 3 characters/digits that exist after X=
*Occurs zero or more times, is short for {0,}X* finds no or several letter X, <sbr /> .* finds any character sequence until a new line occurs
+Occurs one or more times, is short for {1,}X+ Finds one or several letter X
?Occurs no or one times, ? is short for {0,1}X? finds no or exactly one letter X
{X}Occurs X number of times, {} describes the order of the preceding liberal.\d{4} searches for four digits, OR \w{4} for only alphabets, .{4} for any character sequence of length 4.
{X,Y}Occurs between X and Y times.\d{1,4} means it will extract digits minimum 1 time and maximum 4 subsequent digits if exists. Similarly, for OR \w{1,4} means it will extract matching minimum 1 and maximum 4

Now we know to write the regex, but now the question is – “Where to use these extracted values?”

Well! Jmeter provides a postprocessor named Regular Expression Extractor where the extracted regex can be used.

It has below fields:

Body: Select Body as we are trying to extract value from response body of request

Name of create Variable: Add the variable name that you can use in succeeding request.

Regular Expression: Add the regular expression you have extracted from the response body.

Template: Enter $1$, if you have extracted single value
         Enter $1$$2$, if you extract multiple values.

Match No.: Enter 1, if you want to get the first matching value.

          Enter -1, if you want to get all matching values.

Default Value: Enter the value that you want to display if no value is extracted from regex.

We found the place where to use but again question just bumped up – “How to use this regex variables?”

There will be 3 different scenarios we will get while using the regex variables. Let’s see one by one.

1. If there is a single occurrence for 1 extracted value, then we can directly use the variable name.
 – Categories as ${Categories}.

2. If there is a single occurrence for 2 extracted values, the way it can be used is little different. For the Category name we need to pass ${Categories_g1} and for image source you need to use ${Categories_g2}

3. If there are multiple occurrences for 2 extracted values we need to pass as below:
– Category name = ${Categories_1_g1}
– Image source = ${Categories_1_g2}

Do you find the above ways of regex easy and useful? What are more interesting facts of regex in Jmeter and how it is useful to us?

Let us know in the comments.

Recent Posts

Leave a Comment

Start typing and press Enter to search