Working with Regular Expression Extractor

Using Regular Expression Extractor in JMeter

During the course of automating tests many time 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 severs this purpose by pulling out the required values that matches 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 lower case a to z

$ Checks for match at the end of a target string

More are listed below.

While scripting with JMeter, Regular expression extractor is used 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 Perl-type regular expression.

Working with JMeter

To add 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

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 Regular Expression Extractor element

Extracting Single string from the response

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

In order to extract the username, 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 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 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 product id say 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



Author: InApp
We are a custom software development company offering Testing Services, Application Development, Mobility Solutions & more. Customers: Startups - Fortune 500

Leave a Reply

two × 5 =