十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

Regular Expressions in JMeter

上一篇 / 下一篇  2012-03-18 10:20:46 / 个人分类:JMeter

文章来源
  • 文章来源:【转载】

JMeter includes the pattern matching software Apache Jakarta ORO.

?(K Cj+_5U l^0

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

YK;aZ LD0

A summary of the pattern matching characters can be found at http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/package-summary.html51Testing软件测试网fKb[4_9J~a

There is also documentation on an older incarnation of the product at OROMatcher User's guide, which might prove useful.51Testing软件测试网T@v!C*g%^K ?


X9@`h V3~%Z,U051Testing软件测试网B}m~*_B+@

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.51Testing软件测试网p:}dh%F,]:B S4Z*l9yJ

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.51Testing软件测试网\ w YC0N sGPa#{

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

;y Jqu D)x|0
  • "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'.

    2R(D-Y7](U-fK0
  • "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'.51Testing软件测试网n`-rR4l#H

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'.

GGaf@:v0

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

5j7\-y{ |/F0

51Testing软件测试网 MD7E:fLLy

yM I\-U/S,{m j*h0

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

Pct;B{Y3B?M0


qF8`&M$M,o9cd0

H-~(FBT!KN3eKn0

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.

+~o3]KT-E\0


I+gt ^?sm/D051Testing软件测试网'wMh#bv

Examples


Tno!M(O$f0n:u0

?,G4|$Zp0

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.51Testing软件测试网 Iy'{7lJ*R#M;W#z N#V7V

A suitable reqular expression would be:51Testing软件测试网4ba6h2L Fq[^

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

3JMN i XV[W tw0

The special characters above are:

a @;H SB0
  • ( and ) - these enclose the portion of the match string to be returned51Testing软件测试网:{a IMl a8s

  • . - match any character. + - one or more times. ? - don't be greedy, i.e. stop when first match succeeds51Testing软件测试网&C,k4WL$@*X6K e

Note: without the ?, the .+ would continue past the first " until it found the last possible " - probably not what was intended.51Testing软件测试网#H @0~'D1R#E6k7G!k

51Testing软件测试网2z/G o-m|RA
51Testing软件测试网2i m1qa`k"J|F

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.

xRfG2CXmg0

A suitable reqular expression would be:51Testing软件测试网UPD Xf6T7\k l

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

1r.S&@M5gK0

This would create 2 groups, which could be used in the JMeter Regular Expression Extractor template as $1$ and $2$.51Testing软件测试网$\x1@1h z G

The JMeter Regex Extractor saves the values of the groups in additional variables.51Testing软件测试网 p"q1bh]#nE

For example, assume:51Testing软件测试网s4e l7_{U:A&XB]$~5N

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

    5y%d$Xj s7RM0
  • Template: $1$$2$

The following variables would be set:51Testing软件测试网-lUd HE!D`

  • 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} etc51Testing软件测试网;O;M#Y(Ju:_ F `;T*`8gC l


TAG: Jmeter JMeter 正则表达式

 

评分:0

我来说两句

Open Toolbar