Differences between revisions 8 and 9
Revision 8 as of 2005-09-23 23:46:14
Size: 5380
Comment: Add link to pattern matching doc page
Revision 9 as of 2009-09-20 21:56:46
Size: 5394
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
JMeter includes the pattern matching software [http://jakarta.apache.org/oro/ Apache Jakarta ORO]. JMeter includes the pattern matching software [[http://jakarta.apache.org/oro/|Apache Jakarta ORO]].
Line 7: Line 7:
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] 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]]
Line 9: Line 9:
There is also documentation on an older incarnation of the product at [http://www.savarese.org/oro/docs/OROMatcher/index.html OROMatcher User's guide], which might prove useful. There is also documentation on an older incarnation of the product at [[http://www.savarese.org/oro/docs/OROMatcher/index.html|OROMatcher User's guide]], which might prove useful.
Line 13: Line 13:
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. [http://www.oreilly.com 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. 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. [[http://www.oreilly.com|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.
Line 15: Line 15:
There are also a couple of [http://www.oreilly.com/catalog/regex2/chapter/ sample chapters] available on their web-site covering REs in Java and .NET, and the Java chapter has a [http://www.oreilly.com/catalog/regex2/chapter/ch08.pdf section on ORO (PDF)] - worth a look. There are also a couple of [[http://www.oreilly.com/catalog/regex2/chapter/|sample chapters]] available on their web-site covering REs in Java and .NET, and the Java chapter has a [[http://www.oreilly.com/catalog/regex2/chapter/ch08.pdf|section on ORO (PDF)]] - worth a look.
Line 17: Line 17:
It is worth stressing the difference between "contains" and "matches", as used on the [http://jakarta.apache.org/jmeter/usermanual/component_reference.html#Response_Assertion Response Assertion] test element: It is worth stressing the difference between "contains" and "matches", as used on the [[http://jakarta.apache.org/jmeter/usermanual/component_reference.html#Response_Assertion|Response Assertion]] test element:

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:

  • "contains" means that the regular expression matched at least some part of the target, so 'alphabet' "contains" 'ph.b.' because the regular expression matches the substring 'phabe'.

  • "matches" means that the regular expression matched the whole target. So 'alphabet' is "matched" by 'al.*t'. In this case, it is equivalent to wrapping the regular expression in ^ and $, viz '^al.*t$'. However, this is not always the case. For example, the regular expression 'alp|.lp.*' is "contained" in 'alphabet', but does not match 'alphabet'.

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:

  • ( 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 " - 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:

  • Reference Name: is MYREF
  • Regex: name="(.+?)" value="(.+?)"

  • Template: $1$$2$

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} etc

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