Regular Expressions in JMeter

JMeter includes the pattern matching software Apache Jakarta ORO.

There is some documentation for this on the Jakarta web-site.

A summary of the pattern matching characters can be found at http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/package-summary.html

There is also documentation on an older incarnation of the product at OROMatcher User's guide, which might prove useful.

Overview

The pattern matching is very similar to the pattern matching in Perl. A full installation of Perl will include plenty of documentation on regular expressions - look for perlrequick, perlretut, perlre, perlreref. O'Reilly sell a book called "Mastering Regular Expressions" by Jeffrey Friedl which will tell you all you need to know (and a lot more) about regular expressions.

There are also a couple of sample chapters available on their web-site covering REs in Java and .NET, and the Java chapter has a section on ORO (PDF) - worth a look.

It is worth stressing the difference between "contains" and "matches", as used on the Response Assertion test element:

Why? Because when the pattern matcher finds the sequence 'alp' in 'alphabet', it stops trying any other combinations - and 'alp' is not the same as 'alphabet', as it does not include 'habet'.

Note: unlike Perl, there is no need to (i.e. do not) enclose the regular expression in //. So how does one use the Perl modifiers ismx etc if there is no trailing /? The solution is to use Perl5 extended regular expressions, i.e. /abc/i becomes (?i)abc

These resources are not for Jakarta ORO specifically, but are helpful in understanding Regexes in general.

Note that the following testers use engines which may work slightly differently from the Jakarta-ORO (the one currently used by JMeter). However, a lot of regexes will work the same in all the tools.

Examples

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 reqular expression would be:

name="file" value="(.+?)"

The special characters above are:

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

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 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:

The following variables would be set:

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

RegularExpressions (last edited 2009-09-20 21:56:46 by localhost)