Extract multiple dynamic values using one Regular Expression

How to extract more than one dynamic values from a response using the same Regular Expression?

Many of us faced this situation when we need to extract more than 1 dynamic values from a response. The simplest example “If a performance tester wants to fetch three values like Customer Name, Customer ID, and LicenseId from a response, then either he needs to write three separate regular expressions or he needs to write one regular expression that can extract all the values at a time”. Let’s understand this situation more practically. Consider that the response of a page is:

{"domain": "", "localization": "de-DE", "gateWay": "http://public-gateway-cloud-perfmatrix.com", "identityProvider": "http://public-gateway-cloud-perfmatrix.com/api/v1/identity-provider", "customerDNS": "General", "customerName": "PerfMatrix", "staticCacheService": "static-cache-service-cloud-perfmatrix.com", id":"20476939-8465-42f4-adcd-61455d9d830e", "parentId":null, "customerId":"1234567", "orderId": "7bc1779f-735f-410a-ad6f-4f7455d41312", "licenseId":"73fb4c45-771d-4cd1-a6d6-c54406407c78", "name":"HP LoadRunner License", "status":"Active", "childrenCount":0, "licensed":true, "purchaseDate":1511266812000, "expirationDate":1542802812000}

customerName, customerId and licenseId are 3 dynamic values which need to be extracted from the response body. A post-processor regular expression extractor is added under the sampler whose response (above) having all these 3 values.

Extract More than one dynamic value from same regular expression
Figure 01: Regular Expression Extractor fetching 3 dynamic values

The Pattern and Regular Expression to extract required 3 values will be:

Pattern:

LB1(Token1)RB1.*LB2(Token2)RB2.*LB3(Token3)RB3

Note: ‘.*’ is used to ignore the text between two tokens.

Regular Expression:

“customerName”:”(.*?)”,.*”customerId”:”(.*?)”,.*”licenseId”:”(.*?)”,

where:

LB1 =”customerName”:”
Token1 = (.*?)
RB1 = “,
LB2 =”customerId”:”
Token2 = (.*?)
RB2 = “, LB3 =”licenseId”:”
Token3 = (.*?)
RB3 = “,

Template:

It helps to form groups from the response. The syntax is $1$, $2$ and so on. The count depends on the number of dynamic values you are extracting from the response. In our example we need to extract 3 values, hence we used $1$$2$$3$ which will create 3 groups or variables to store the extracted values, so:

$1$ refers to value of “customerName”:”(.*?)”
$2$ refers to value of “customerId”:”(.*?)”
$3$ refers to value of “licenseId”:”(.*?)”,

Output:

multiValueRegEx_g1 = PerfMatrix
multiValueRegEx_g2 = 1234567
multiValueRegEx_g3 = 73fb4c45-771d-4cd1-a6d6-c54406407c78

How to use Reg-Ex variable having multiple extracted values in the script?

You can use these variables with the help of groups which is formed by the template (explained above). Hence when you want to pass the value of customer name, then you need to use ${multiValueRegEx_g1 }, for customer ID you can use ${multiValueRegEx_g2} and for license ID you can use ${multiValueRegEx_g3}i.e just append _g<groupNumber> to regular expression variable.


Leave a Reply

Your email address will not be published. Required fields are marked *