Regular Expression Tutorial for Apache JMeter Tests

Regular Expression Tutorial for Apache JMeter Tests

A regular expression is a special text string for describing a search pattern. Regular expressions have a reputation for being tough, but…

Regular Expression Tutorial for Apache JMeter Tests

A regular expression is a special text string for describing a search pattern. Regular expressions have a reputation for being tough, but that all depends on how you approach them. You can start from expressions as simple as this: \d which helps you identify a character that matches any digit from 0 to 9, to something complex, like: ^(\(\d{3}\)|^\d{3}[.-]?)?\d{3}[.-]?\d{4}$

While testing web applications, you need to extract information from HTML, JSON or XML response body or validate fields. You can always use CSS Selectors or JSON/XML Extractors. Besides those using regular expressions in JMeter is very powerful.

JMeter’s Regular Expression extractor works as a Post Processor. When you use Regular expression extractor as a child element of Sampler, you need to fill out 5 fields.

  1. Name of created variables: Extracted data will be assigned to that jmeter variable, you will be able to access it by using ${var} format.
  2. Regular Expression: This is where you write your regex
  3. Template: You use it to give the index number of the extracted variable that you will use by using $1$, $2$ format.
  4. Match no: In case many results are found, the item on that index will be used. For random choosing, you should use 0 as we do in JSON, CSS or XML extractors.
  5. Default Value: In case no value is found, this will be used.

If the match number is set to a valid 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

Let’s see it in a working example.

We have a sampler to make a request to a web service. This web service will return a JSON data. We will extract the job names from that endpoint.

Our regular expression is “DisplayString”:”(.+?)”

This expression will capture the value where “(.+?)” expression is situated. The basic reading of the expression is below:

( and )
these enclose the portion of the match string to be returned
match any character
one or more times
don’t be greedy, stop when the first match succeeds

Let’s add a Debug Sampler to our JMeter script to debug the values by selecting Add->Sampler->Debug Sampler. You can find detailed information about Debug sample in this article 3 Ways of Debugging JMeter Scripts

Our variable name was jobName, then Regular Expression Extractor created those 4 variables for our usage.

This example showed you how to extract one value from a response. You might want to extract all the values and keep them in JMeter’s memory during the whole execution. The only thing you need to do is change the Match No from 0 to -1.

Now run your test one more time and check the result of your Debug script. As you can see, there are multiple values.

Now you need to use ${variableName_index} format to use to call the element on an index.

In case you want to go deep dive with regular expression there are many online tools to teach you regular expressions like

Happy testing!