Extracting variables using Regular Expressions in JMeter

I’m trying to web performance test an application that regularly changes URLs. We’ll use regular expressions to figure out what that URL should be, store it as a JMeter variable, and use it in a sampler.

So first of all, we do a search for a book at a known page at the following address /cgi-bin/.exe/ENQ/CAT/BIBENQ. This address and the screenshots have been edited to remove the application name and the host address. From the result of this we’ve received some data as you can see in the second screenshot.

This data comes in the format of a URL: /cgi-bin/.exe/FULL/CAT/BIBENQ/XXXXX/410986,1, where XXXXX is a five digit number that changes constantly. This means we can’t set up a sampler with a static address, as the address won’t always be correct.

So for the sampler with the result data, we’ll right click and add Post Processor > Regular Expression Extractor. The fields in details are:

  • Reference Name – This is the variable name, in my case FLUTEURL. Elsewhere in JMeter, we’ll reference this variabvle as ${FLUTEURL}
  • Regular Expression – This is the regular expression that will extract the data we’re looking for. Brackets are put around the value we want to extract. So in our example we have /cgi-bin/.exe/FULL/CAT/BIBENQ/(.+?)/410986,1 or, we’re looking for the value that sits between the slash after BIBENQ and the slash before 410986,1. This will return in our case a five digit number, XXXXX
  • Template – Your regular expression could have multiple values extracted, in our case we have just one, and so we’re using the first template, or $1$
  • Match No. – If there are multiple matches, which one should be extracted? In our case, we want the first match, or 1
  • Default value – If a value cannot be extracted, what should we put in place instead? You could have ERROR, but I just leave it blank

So we now have a JMeter variable called ${FLUTEURL} with a value of say, 12345. How would we use this? In my case, for the path for the next sampler is:

  • /cgi-bin/.exe/FULL/CAT/BIBENQ/${FLUTEURL}/410986,1

So in execution the URL the sampler requests is /cgi-bin/.exe/FULL/CAT/BIBENQ/12345/410986,1 and the sampler works correctly.

In summary, we’ve extracted a value from a page, stored it as a JMeter variable, and then used that variable in another sampler.