DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Web App Load Testing Using Maven Plugins for Apache JMeter, and Analyzing the Results
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Modes and Modality in Performance Testing
  • Understanding the Power of Coefficient of Variation in Software Performance Testing

Trending

  • Why Stable RAG Answers Can Still Hide Unstable Evidence
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Regular Expression Tutorial for Apache JMeter Tests

Regular Expression Tutorial for Apache JMeter Tests

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

By 
Canberk Akduygu user avatar
Canberk Akduygu
·
Nov. 20, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Apache JMeter Testing

Published at DZone with permission of Canberk Akduygu. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Web App Load Testing Using Maven Plugins for Apache JMeter, and Analyzing the Results
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases
  • Modes and Modality in Performance Testing
  • Understanding the Power of Coefficient of Variation in Software Performance Testing

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook