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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 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
  • Steps To Integrate Selenium Test Case With Apache JMeter

Trending

  • Measuring the Impact of AI on Software Engineering Productivity
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Agentic AI for Automated Application Security and Vulnerability Management
  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.1K 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

  • 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
  • Steps To Integrate Selenium Test Case With Apache JMeter

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!