mirror of https://github.com/apache/jmeter.git
255 lines
15 KiB
HTML
255 lines
15 KiB
HTML
<!DOCTYPE html SYSTEM "about:legacy-compat">
|
|
<html lang="en"><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-15"><title>Apache JMeter
|
|
-
|
|
User's Manual: Regular Expressions</title><meta name="viewport" content="width=device-width, initial-scale=1"><link href="http://fonts.googleapis.com/css?family=Merriweather:400normal" rel="stylesheet" type="text/css"><link rel="stylesheet" type="text/css" href="../css/new-style.css"></head><body role="document"><a href="#content" class="hidden">Main content</a><div class="header"><!--
|
|
APACHE LOGO
|
|
--><div><a href="http://www.apache.org"><img title="Apache Software Foundation" width="290" height="75" src="../images/asf-logo.png" alt="Logo ASF"></a></div><!--
|
|
PROJECT LOGO
|
|
--><div><a href="http://jmeter.apache.org/"><img src="../images/logo.jpg" alt="Apache JMeter"></a></div><div class="twitter"><div><a href="https://twitter.com/share" class="twitter-share-button" data-text="Powerful Load Testing with Apache #JMeter" data-via="ApacheJMeter" data-lang="en-gb" data-size="large">Tweet</a><script>
|
|
(function(d,s,id){
|
|
var js,
|
|
fjs=d.getElementsByTagName(s)[0],
|
|
p=/^http:/.test(d.location)?'http':'https';
|
|
if (!d.getElementById(id)) {
|
|
js=d.createElement(s);
|
|
js.id=id;
|
|
js.src=p+'://platform.twitter.com/widgets.js';
|
|
fjs.parentNode.insertBefore(js,fjs);
|
|
}
|
|
})(document, 'script', 'twitter-wjs');
|
|
</script></div><div><a href="https://twitter.com/ApacheJMeter" class="twitter-follow-button" data-show-count="false" data-lang="en-gb" data-size="large">Follow</a><script>(function(d,s,id){
|
|
var js,
|
|
fjs=d.getElementsByTagName(s)[0],
|
|
p=/^http:/.test(d.location)?'http':'https';
|
|
if (!d.getElementById(id)) {
|
|
js=d.createElement(s);
|
|
js.id=id;
|
|
js.src=p+'://platform.twitter.com/widgets.js';
|
|
fjs.parentNode.insertBefore(js,fjs);
|
|
}
|
|
})(document, 'script', 'twitter-wjs');
|
|
</script></div></div><div class="banner"><iframe src="http://www.apache.org/ads/bannerbar.html" style="border-width:0;" frameborder="0" scrolling="no"></iframe><div class="clear"></div></div></div><div class="nav"><ul class="menu"><li><div class="menu-title">About</div><ul><li><a href="../index.html">Overview</a></li><li><a href="http://twitter.com/ApacheJMeter">JMeter on Twitter<img src="../images/twitter.png" alt="Icon for JMeter on Twitter"></a></li><li><a href="../issues.html">Issue Tracking</a></li><li><a href="http://projects.apache.org/feeds/rss/jmeter.xml">Subscribe to What's New</a></li><li><a href="http://www.apache.org/licenses/">License</a></li></ul></li></ul><ul class="menu"><li><div class="menu-title">Download</div><ul><li><a href="../download_jmeter.cgi">Download Releases</a></li><li><a href="../nightly.html">Developer (Nightly) Builds</a></li></ul></li></ul><ul class="menu"><li><div class="menu-title">Documentation</div><ul><li><a href="../changes.html">Changes per version</a></li><li><a href="../usermanual/get-started.html">Get Started</a></li><li><a href="../usermanual/index.html">User Manual</a></li><li><a href="../usermanual/best-practices.html">Best Practices</a></li><li><a href="../usermanual/component_reference.html">Component Reference</a></li><li><a href="../usermanual/functions.html">Functions Reference</a></li><li><a href="../api/index.html">Javadocs</a></li><li><a href="../building.html">Building JMeter and Add-Ons</a></li><li><a href="http://wiki.apache.org/jmeter">JMeter Wiki</a></li><li><a href="http://wiki.apache.org/jmeter/JMeterFAQ">FAQ (Wiki)</a></li></ul></li></ul><ul class="menu"><li><div class="menu-title">Tutorials (PDF format)</div><ul><li><a href="../usermanual/jmeter_distributed_testing_step_by_step.pdf">Distributed Testing</a></li><li><a href="../usermanual/jmeter_proxy_step_by_step.pdf">Recording Tests</a></li><li><a href="../usermanual/junitsampler_tutorial.pdf">JUnit Sampler</a></li><li><a href="../usermanual/jmeter_accesslog_sampler_step_by_step.pdf">Access Log Sampler</a></li><li><a href="../extending/jmeter_tutorial.pdf">Extending JMeter</a></li></ul></li></ul><ul class="menu"><li><div class="menu-title">Community</div><ul><li><a href="http://wiki.apache.org/jmeter/JMeterCommitters">Contributors</a></li><li><a href="../mail.html">Mailing Lists</a></li><li><a href="../svnindex.html">SVN Repositories</a></li></ul></li></ul><ul class="menu"><li><div class="menu-title">Foundation</div><ul><li><a href="http://www.apache.org/">ASF</a></li><li><a href="http://www.apache.org/foundation/getinvolved.html">Get Involved in the ASF</a></li><li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li><li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li></ul></li></ul></div><div class="main" id="content"><ul class="pagelinks"><li><a href="functions.html">< Prev</a></li><li><a href="../index.html">Index</a></li><li><a href="hints_and_tips.html">Next ></a></li></ul><div class="section"><h1 id="regex">20. Regular Expressions<a class="sectionlink" href="#regex" title="Link to here">¶</a></h1>
|
|
<div class="subsection"><h2>20.1 Overview<a class="sectionlink" href="#overview" title="Link to here">¶</a></h2>
|
|
<p>
|
|
JMeter includes the pattern matching software <a href="http://attic.apache.org/projects/jakarta-oro.html">Apache Jakarta ORO</a>
|
|
<br>
|
|
There is some documentation for this on the Jakarta web-site, for example
|
|
<a href="http://archimedes.fas.harvard.edu/scrapbook/jakarta-oro-2.0.6/docs/api/org/apache/oro/text/regex/package-summary.html">
|
|
a summary of the pattern matching characters</a>
|
|
</p>
|
|
<p>
|
|
There is also documentation on an older incarnation of the product at
|
|
<a href="http://www.savarese.org/oro/docs/OROMatcher/index.html">OROMatcher User's guide</a>, which might prove useful.
|
|
</p>
|
|
<p>
|
|
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.
|
|
</p>
|
|
<p>
|
|
It is worth stressing the difference between "contains" and "matches", as used on the Response Assertion test element:
|
|
</p>
|
|
<ul>
|
|
<li>
|
|
"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'.
|
|
</li>
|
|
<li>
|
|
"matches" means that the regular expression matched the whole target.
|
|
So 'alphabet' is "matched" by 'al.*t'.
|
|
</li>
|
|
</ul>
|
|
<p>In this case, it is equivalent to wrapping the regular expression in ^ and $, viz '^al.*t$'.
|
|
</p>
|
|
<p>However, this is not always the case.
|
|
For example, the regular expression 'alp|.lp.*' is "contained" in 'alphabet', but does not match 'alphabet'.
|
|
</p>
|
|
<p>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'.
|
|
</p>
|
|
<p>
|
|
Note: unlike Perl, there is no need to (i.e. do not) enclose the regular expression in //.
|
|
</p>
|
|
<p>
|
|
So how does one use the modifiers ismx etc if there is no trailing /?
|
|
The solution is to use <i>extended regular expressions</i>, i.e. /abc/i becomes (?i)abc.
|
|
See also <a href="#placement">Placement of modifiers</a> below.
|
|
</p>
|
|
</div>
|
|
<div class="subsection"><h2>20.2 Examples<a class="sectionlink" href="#examples" title="Link to here">¶</a></h2>
|
|
<h3>Extract single string</h3>
|
|
<p>
|
|
Suppose you want to match the following portion of a web-page:
|
|
<br>
|
|
<span class="code">name="file" value="readme.txt"></span>
|
|
<br>
|
|
and you want to extract <span class="code">readme.txt</span>.
|
|
<br>
|
|
A suitable regular expression would be:
|
|
<br>
|
|
<span class="code">name="file" value="(.+?)"></span>
|
|
<p>
|
|
The special characters above are:
|
|
</p>
|
|
<ul>
|
|
<li>( and ) - these enclose the portion of the match string to be returned</li>
|
|
<li>. - match any character</li>
|
|
<li>+ - one or more times</li>
|
|
<li>? - don't be greedy, i.e. stop when first match succeeds</li>
|
|
</ul>
|
|
<p>
|
|
Note: without the ?, the .+ would continue past the first <span class="code">"></span>
|
|
until it found the last possible <span class="code">"></span> - which is probably not what was intended.
|
|
</p>
|
|
<p>
|
|
Note: although the above expression works, it's more efficient to use the following expression:
|
|
<br>
|
|
<span class="code">name="file" value="([^"]+)"></span>
|
|
where<br>
|
|
[^"] - means match anything except "<br>
|
|
In this case, the matching engine can stop looking as soon as it sees the first <span class="code">"</span>,
|
|
whereas in the previous case the engine has to check that it has found <span class="code">"></span> rather than say <span class="code">" ></span>.
|
|
</p>
|
|
<h3>Extract multiple strings</h3>
|
|
<p>
|
|
Suppose you want to match the following portion of a web-page:<br>
|
|
<span class="code">name="file.name" value="readme.txt"</span>
|
|
and you want to extract both <span class="code">file.name</span> and <span class="code">readme.txt</span>.
|
|
<br>
|
|
A suitable reqular expression would be:
|
|
<br>
|
|
<span class="code">name="([^"]+)" value="([^"]+)"</span>
|
|
<br>
|
|
This would create 2 groups, which could be used in the JMeter Regular Expression Extractor template as $1$ and $2$.
|
|
</p>
|
|
<p>
|
|
The JMeter Regex Extractor saves the values of the groups in additional variables.
|
|
</p>
|
|
<p>
|
|
For example, assume:
|
|
</p>
|
|
<ul>
|
|
<li>Reference Name: MYREF</li>
|
|
<li>Regex: name="(.+?)" value="(.+?)"</li>
|
|
<li>Template: $1$$2$</li>
|
|
</ul>
|
|
<div class="clear"></div><div class="note">Do not enclose the regular expression in / /</div><div class="clear"></div>
|
|
<p>
|
|
The following variables would be set:
|
|
</p>
|
|
<ul>
|
|
<li>MYREF: file.namereadme.txt</li>
|
|
<li>MYREF_g0: name="file.name" value="readme.txt"</li>
|
|
<li>MYREF_g1: file.name</li>
|
|
<li>MYREF_g2: readme.txt</li>
|
|
</ul>
|
|
These variables can be referred to later on in the JMeter test plan, as ${MYREF}, ${MYREF_g1} etc
|
|
</p>
|
|
</div>
|
|
<div class="subsection"><h2>20.3 Line mode<a class="sectionlink" href="#line_mode" title="Link to here">¶</a></h2>
|
|
<p>The pattern matching behaves in various slightly different ways,
|
|
depending on the setting of the multi-line and single-line modifiers.
|
|
Note that the single-line and multi-line operators have nothing to do with each other;
|
|
they can be specified independently.
|
|
</p>
|
|
<h3>Single-line mode</h3>
|
|
<p>
|
|
Single-line mode only affects how the '.' meta-character is interpreted.
|
|
</p>
|
|
<p>
|
|
Default behaviour is that '.' matches any character except newline.
|
|
In single-line mode, '.' also matches newline.
|
|
</p>
|
|
|
|
<h3>Multi-line mode</h3>
|
|
<p>
|
|
Multi-line mode only affects how the meta-characters '^' and '$' are interpreted.
|
|
</p>
|
|
<p>
|
|
Default behaviour is that '^' and '$' only match at the very beginning and end of the string.
|
|
When Multi-line mode is used, the '^' metacharacter matches at the beginning of every line,
|
|
and the '$' metacharacter matches at the end of every line.</p>
|
|
|
|
</div>
|
|
|
|
<div class="subsection"><h2>20.4 Meta characters<a class="sectionlink" href="#meta_chars" title="Link to here">¶</a></h2>
|
|
<p>
|
|
Regular expressions use certain characters as meta characters - these characters have a special meaning to the RE engine.
|
|
Such characters must be escaped by preceeding them with \ (backslash) in order to treat them as ordinary characters.
|
|
Here is a list of the meta characters and their meaning (please check the ORO documentation if in doubt).
|
|
</p>
|
|
<ul>
|
|
<li>( ) - grouping</li>
|
|
<li>[ ] - character classes</li>
|
|
<li>{ } - repetition</li>
|
|
<li>* + ? - repetition</li>
|
|
<li>. - wild-card character</li>
|
|
<li>\ - escape character</li>
|
|
<li>| - alternatives</li>
|
|
<li>^ $ - start and end of string or line</li>
|
|
</ul>
|
|
<div class="clear"></div><div class="note">
|
|
<p>Please note that ORO does not support the \Q and \E meta-characters.
|
|
[In other RE engines, these can be used to quote a portion of an RE so that the meta-characters stand for themselves.]
|
|
You can use function to do the equivalent, see <a href="functions.html#__escapeOroRegexpChars">${__escapeOroRegexpChars(valueToEscape)}</a>.
|
|
</p>
|
|
</div><div class="clear"></div>
|
|
<p>
|
|
The following Perl5 extended regular expressions are supported by ORO.
|
|
|
|
<dl>
|
|
<dt>(?#text)</dt>
|
|
<dd>An embedded comment causing text to be ignored.</dd>
|
|
<dt>(?:regexp)</dt>
|
|
<dd>Groups things like "()" but doesn't cause the group match to be saved.</dd>
|
|
<dt>(?=regexp)</dt>
|
|
<dd>A zero-width positive lookahead assertion. For example, \w+(?=\s) matches a word followed by whitespace, without including whitespace in the MatchResult.</dd>
|
|
<dt>(?!regexp)</dt>
|
|
<dd>A zero-width negative lookahead assertion. For example foo(?!bar) matches any occurrence of "foo" that isn't followed by "bar". Remember that this is a zero-width assertion, which means that a(?!b)d will match ad because a is followed by a character that is not b (the d) and a d follows the zero-width assertion.</dd>
|
|
<dt>(?imsx)</dt>
|
|
<dd>One or more embedded pattern-match modifiers. i enables case insensitivity, m enables multiline treatment of the input, s enables single line treatment of the input, and x enables extended whitespace comments.</dd>
|
|
</dl>
|
|
<b>Note that <span class="code">(?<=regexp)</span> - lookbehind - is not supported.</b>
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="subsection"><h2>20.5 Placement of modifiers<a class="sectionlink" href="#placement" title="Link to here">¶</a></h2>
|
|
<p>
|
|
Modifiers can be placed anywhere in the regex, and apply from that point onwards.
|
|
[A bug in ORO means that they cannot be used at the very end of the regex.
|
|
However they would have no effect there anyway.]
|
|
</p>
|
|
<p>
|
|
The single-line (?s) and multi-line (?m) modifiers are normally placed at the start of the regex.
|
|
</p>
|
|
<p>
|
|
The ignore-case modifier (?i) may be usefully applied to just part of a regex,
|
|
for example:
|
|
<pre>
|
|
Match ExAct case or (?i)ArBiTrARY(?-i) case
|
|
</pre>
|
|
</p>
|
|
</div>
|
|
</div><div class="section"><h1 id="testing_expressions">20.6 Testing Regular Expressions<a class="sectionlink" href="#testing_expressions" title="Link to here">¶</a></h1>
|
|
<p>
|
|
Since JMeter 2.4, the listener <a href="component_reference.html#View_Results_Tree">View Results Tree</a>
|
|
include a RegExp Tester to test regular expressions directly on sampler response data.
|
|
</p>
|
|
<p>
|
|
There is a <a href="http://www.regexplanet.com/advanced/java/index.html">Website</a> to test Java Regular expressions.
|
|
</p>
|
|
<p>
|
|
Another approach is to use a simple test plan to test the regular expressions.
|
|
The Java Request sampler can be used to generate a sample, or the HTTP Sampler can be used to load a file.
|
|
Add a Debug Sampler and a Tree View Listener and changes to the regular expression can be tested quickly,
|
|
without needing to access any external servers.
|
|
</p>
|
|
</div><ul class="pagelinks"><li><a href="functions.html">< Prev</a></li><li><a href="../index.html">Index</a></li><li><a href="hints_and_tips.html">Next ></a></li></ul></div><div class="footer"><div class="copyright">
|
|
Copyright ©
|
|
1999 –
|
|
2015
|
|
, Apache Software Foundation
|
|
</div><div class="trademarks">Apache, Apache JMeter, JMeter, the Apache
|
|
feather, and the Apache JMeter logo are
|
|
trademarks of the
|
|
Apache Software Foundation.
|
|
</div></div></body></html> |