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

Regular Expression Tutorial for Apache JMeter Tests

DZone 's Guide to

Regular Expression Tutorial for Apache JMeter Tests

Learn how to use JMeter's Regular Expression extractor as a Post Processor in your load testing.

· Performance Zone ·
Free Resource

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 \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, but using regular expressions in JMeter is very powerful.

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

  1. Name of created variables: Extracted data will be assigned to that JMeter variable; you will be able to access it by using the ${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 JSON data. We will extract the job names from that endpoint.

jmeter

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 3 Ways of Debugging JMeter Scripts.

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

jobName=Sous Chef 
jobName_g=1 
jobName_g0="DisplayString":"Sous Chef"
 jobName_g1=Sous Chef


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 https://regexr.com/.

Happy testing!

Topics:
jmeter ,performance testing ,load testing ,tutorial ,performance ,regex ,regular expressions

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}