Working with Regular Expression Extractor in JMeter

Using Regular Expression Extractor in JMeter

During automating tests many times, the test scripts depend on input values that are generated during the test run. These values can be stored in a variable but sometimes the test requires only a part of this value. In such cases, the need for a string extractor is felt.

Regular Expression Extractor serves this purpose by pulling out the required values that match the pattern.
[ ] Matches anything within the square bracket

– Dash inside a square bracket specifies the range e.g [ 0-9] means all digits from 0 to 9

^ Negates the expression e.g [^ a-z] means everything except lowercase a to z

$ Checks for the match at the end of a target string

More are listed below.

While scripting with JMeter, a Regular expression extractor is used to retrieve the values from the server response. This value is passed as a parameter to the While controller & IF controller. It can also be used to replace any pre-defined variable.

The regular expression used is a Perl-type regular expression.

Working with JMeter

To add a regular expression extractor element to a test plan in JMeter:

Right-click the sampler element (request to the server from which the value needs to be extracted)
Select Add option -> Post Processors -> Regular expression extractor

An explanation of the Regular expression extractor element is detailed in the link

http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor

How to extract Single or multiple strings using the Regular Expression Extractor element

Extracting a Single string from the response

Consider an example, where a user successfully logins into an online shopping website and is navigated to the user’s home page where the name ‘Welcome Username’ is displayed.

In order to extract the username, the below R.E can be used:

Reference Name: Username

Regular Expression: Welcome (.+?)

Template: $1$

Match No. (0 for Random): 1

Default Value: match not found

Note: The special characters above mean:

( ) encloses the portion of the match string to be returned

. -> match for any character. + -> one or more times.

? stop when the first match is found

Without the ? the .+ would continue until it finds the last possible match.

Extracting Multiple Strings from the response

Consider a scenario where the user selects an item; it has a product id and a category id. To extract both the ids, the below R.E can be used

Reference Name: My_ID

Regular Expression: Product_ID = (.+?)\&Category_ID = (.+?)

Template: $1$$2$

Match No. (0 for Random): 1

Default Value: match not found

Since we need to extract two values from the response, two groups are created. So the template has $1$$2$. The JMeter Regex Extractor saves the values of the groups in additional variables.

The following variables would be set as:

My_ID -> PR_001CAT_001

My_ID _g0 -> Product_ID =” PR_001″ Category_ID =” CAT_001″

My_ID _g1 -> PR_001

My_ID _g2 -> CAT_001

These variables can be later referred to in the JMeter test plan, as ${MY_ID_g1}, ${MYREF_g2}.

Extracting only numbers from the String

Consider a case where we need to only extract the numbers, for example, the product id says PR_001.

To extract 001, the below R.E can be used

Reference Name: ProductID

Regular Expression: Product_ID = “PR_(.+?)”

Template: $1$

Match No. (0 for Random): 1

Default Value: match not found

Have questions? Contact the software testing experts at InApp to learn more.