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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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.

Canberk Akduygu user avatar by
Canberk Akduygu
·
Nov. 20, 18 · Tutorial
Like (1)
Save
Tweet
Share
13.16K 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.

Popular on DZone

  • How Observability Is Redefining Developer Roles
  • OpenID Connect Flows
  • Iptables Basic Commands for Novice
  • Distributed SQL: An Alternative to Database Sharding

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: