如何从jmeter正则表达式中获取最后一个值?

时间:2016-11-21 12:01:29

标签: regex jmeter

enter image description here

我使用具有以下配置的正则表达式来提取 id 值:

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

但我没有得到最后的价值我随机获得价值。请建议如何获取最后一个id值。

下面给出了响应,并希望从响应中提取N1600-2016-11-18-1479503469487值。

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"}]

添加Beanshell后

enter image description here

2 个答案:

答案 0 :(得分:1)

A。如果您确定第N个元素(比如第4个)是last value every time,那么您可以hard code Match no. as 4中的值Regex: `"id":"(.*?)"\,"batch":{"id":"(.*?)"}` Template is `$1$` match value is -1 // to store all matched values

B。如果您不确定它是否总是Nth(4)元素,请按以下步骤操作:

在RegExExtractor中提及以下值:

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

将BeanShell PostProcessor作为子项添加到同一个采样器中,并添加以下代码:

${lastID}

您可以按如下方式引用最后一个值:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

参考图片:

RegExExtractor:

enter image description here

BeanShell PostProcessor:

enter image description here

答案 1 :(得分:0)

为什么不将响应解析为JSON对象?如果将来修改json对象,它将比使用正则表达式更易于维护。