Thursday 20 March 2014

Extract single string in regular expression extractor in Jmeter correlation

Extract single string

Suppose you want to match the following portion of a web-page:
name="file" value="readme.txt">
and you want to extract readme.txt .
A suitable regular expression would be:
name="file" value="(.+?)">

The special characters above are:
  • ( and ) - these enclose the portion of the match string to be returned
  • . - match any character
  • + - one or more times
  • ? - don't be greedy, i.e. stop when first match succeeds

Note: without the ?, the .+ would continue past the first "> until it found the last possible "> - which is probably not what was intended.

Note: although the above expression works, it's more efficient to use the following expression: 

name="file" value="([^"]+)"> where
[^"] - means match anything except "
In this case, the matching engine can stop looking as soon as it sees the first " , whereas in the previous case the engine has to check that it has found "> rather than say " > .
Extract multiple strings

Suppose you want to match the following portion of a web-page:
name="file.name" value="readme.txt" and you want to extract both file.name and readme.txt .
A suitable reqular expression would be:
name="([^"]+)" value="([^"]+)"
This would create 2 groups, which could be used in the JMeter Regular Expression Extractor template as $1$ and $2$.

The JMeter Regex Extractor saves the values of the groups in additional variables.

For example, assume:
Reference Name: MYREF
Regex: name="(.+?)" value="(.+?)"
Template: $1$2$


Do not enclose the regular expression in / /

The following variables would be set:
MYREF: file.namereadme.txt
MYREF_g0: name="file.name" value="readme.txt"
MYREF_g1: file.name
MYREF_g2: readme.txt


These variables can be referred to later on in the JMeter test plan, as ${MYREF}, ${MYREF_g1

No comments: