Get Knowledge from Video instead of Content:

Monday, 24 October 2016

Extract multiple values using same Regular Expression


How to extract more than one value using the same regular expression?

Many of us face this situation when we need to extract more than 1 dynamic values from the response which need to be passed in the successive requests. The simplest example "If a performance tester wants to fetch three values which are Customer Name, Customer ID, and LicenseId, then either he needs to write two regular expressions or he needs to write such an expression which 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}
A post-processor regular expression extractor is added under the sampler whose response (above) having all these 3 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 sysntex 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 and 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 customer name, you can use ${multiValueRegEx_g1 }, for customer ID you can use ${multiValueRegEx_g2} and for license ID you can use ${multiValueRegEx_g3} as a variable i.e just append _g<groupNumber> to regular expression variable. 

To get more understanding of Regular Expression and correlation, refer below links:

No comments :

Post a Comment