tutorials city
tutorials city

Reputation: 21

How to get the last value from jmeter Regular Expression?

enter image description here

I have used regular expression with the following configuration to extract id value:

Regex: `"id":"(.*?)"\,"batch":{"id":"(.*?)"}` 
Template is `$1$` 
match value is `N` 

but I am not getting last value I am getting value on a random basis. please suggest how to get last id value.

The response is given below and wants to extract N1600-2016-11-18-1479503469487 value from the response.

14T21:00:00Z","version":1479157741885,"recordCount":92054,"statusIdentifier":"END"},{"id":"N1600-2016-11-15-1479244284441","batch":{"id":"N1600"},"asOf":"2016-11-15T21:00:00Z","runDate":"2016-11-15T21:00:00Z","version":1479244284441,"recordCount":91838,"statusIdentifier":"END"},{"id":"N1600-2016-11-16-1479330679449","batch":{"id":"N1600"},"asOf":"2016-11-16T21:00:00Z","runDate":"2016-11-16T21:00:00Z","version":1479330679449,"recordCount":91796,"statusIdentifier":"END"},{"id":"N1600-2016-11-17-1479417271814","batch":{"id":"N1600"},"asOf":"2016-11-17T21:00:00Z","runDate":"2016-11-17T21:00:00Z","version":1479417271814,"recordCount":91761,"statusIdentifier":"END"},{"id":"N1600-2016-11-18-1479503469487","batch":{"id":"N1600"},"asOf":"2016-11-18T21:00:00Z","runDate":"2016-11-18T21:00:00Z","version":1479503469487,"recordCount":91693,"statusIdentifier":"END"}]

After adding Beanshell

enter image description here

Upvotes: 2

Views: 3489

Answers (2)

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

A. If you are sure that Nth element (say 4th) is the last value every time, then you can hard code the value in Match no. as 4.

B. If you are not sure it is always Nth (4) element, then do as follows:

Mention following values in RegExExtractor:

Regex: `"id":"(.*?)"\,"batch":{"id":"(.*?)"}` 
Template is `$1$` 
match value is -1  // to store all matched values

Add BeanShell PostProcessor as a child to the same sampler and add the following code:

int size = Integer.parseInt(vars.get("ID_matchNr")); // to get size of matched values
log.info("size:" + size ); // you can remove log stmts once it is working
String newId = "ID_" + size;
String lastId = vars.get(newId); // to retrieve the value
log.info("last id: " + lastId);
vars.put("lastID", lastId); // to store the value

you can refer the last value as follows:

${lastID}

Reference Image:

RegExExtractor:

enter image description here

BeanShell PostProcessor:

enter image description here

Upvotes: 2

Why don't you parse the response as a JSON Object? It's going to be more maintainable than using regular expressions if your json object is modified in a future.

Upvotes: 0

Related Questions