Interview Questions

How to Create Regular Expressions

Apache JMeter FAQ


(Continued from previous question...)

How to Create Regular Expressions

Ensure you have an exact copy of the source document that you want to operate on
One way to do this is to attach a Save Responses to a file Listener to the Sampler, and run the test to create a copy of the resource.
You can then use the HTTP Sampler with the "file:" protocol to retrieve the page at any time. .

Find the variable part that you want to extract (in the file, or in the Tree View Listener), and start by using that as the regular expression.

For example, suppose you want to find the value from the following snippet:

input type="hidden" name="secret" value="CAFEBABE.12345(3)"

Start with exactly that as the regular expression, and check that it works, for example in the Tree View Listener Regex tester panel.

If not, examine the expression for any meta-characters (characters that have a special meaning in regexes).In this case, the only special characters are the "." and the parentheses "(" and ")".These need to be escaped, by being prefixed with "\". We now have:

input type="hidden" name="secret" value="CAFEBABE\.12345\(3\)"

This should now match the whole phrase.

The next stage is to tell the regex processor which part of the section you want to use. This is easy, just enclose the characters in parentheses.

So assume you want to match just

CAFEBABE.12345

Your regular expression then becomes:

input type="hidden" name="secret" value="(CAFEBABE\.12345)\(3\)"

(Continued on next question...)

Other Interview Questions