转自:

http://jmeter.apache.org/usermanual/regular_expressions.html

21.1 Overview

JMeter includes the pattern matching software Apache Jakarta ORO There is some documentation for this on the Jakarta web-site, for example  a summary of the pattern matching characters

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

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 and perlreref.

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

 
Unlike Perl, there is no need to (i.e. do not) enclose the regular expression in //.
 

So how does one use the modifiers ismx etc. if there is no trailing /?  The solution is to use extended regular expressions, i.e. /abc/i becomes (?i)abc. See also Placement of modifiers below.

21.2 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 regular 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 "> - which is probably not what was intended.

Note: although the above expression works, it's more efficient to use the following expression: name="file" value="([^"]+)"> where [^"] - means match anything except " In this case, the matching engine can stop looking as soon as it sees the first ",  whereas in the previous case the engine has to check that it has found "> rather than say " >.

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 both file.name and readme.txt. A suitable regular 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: MYREF
  • Regex: name="(.+?)" value="(.+?)"
  • Template: $1$$2$
 
Do not enclose the regular expression in / /
 

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.

21.3 Line mode

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.

Single-line mode

Single-line mode only affects how the '.' meta-character is interpreted.

Default behaviour is that '.' matches any character except newline.  In single-line mode, '.' also matches newline.

Multi-line mode

Multi-line mode only affects how the meta-characters '^' and '$' are interpreted.

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.

21.4 Meta characters

Regular expressions use certain characters as meta characters - these characters have a special meaning to the RE engine. Such characters must be escaped by preceding 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).

( and )
grouping
[ and ]
character classes
{ and }
repetition
*, + and ?
repetition
.
wild-card character
\
escape character
|
alternatives
^ and $
start and end of string or line
 
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 ${__escapeOroRegexpChars(valueToEscape)}.
 

The following Perl5 extended regular expressions are supported by ORO.

(?#text)
An embedded comment causing text to be ignored.
(?:regexp)
Groups things like "()" but doesn't cause the group match to be saved.
(?=regexp)
A zero-width positive lookahead assertion. For example, \w+(?=\s) matches a word followed by whitespace, without including whitespace in the MatchResult.
(?!regexp)
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.
(?imsx)
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.

Note that (?<=regexp) - lookbehind - is not supported.

21.5 Placement of modifiers

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

The single-line (?s) and multi-line (?m) modifiers are normally placed at the start of the regex.

The ignore-case modifier (?i) may be usefully applied to just part of a regex, for example:

Match ExAct case or (?i)ArBiTrARY(?-i) case

would match Match ExAct case or arbitrary case as well as Match ExAct case or ARBitrary case, but not Match exact case or ArBiTrARY case.

21.6 Testing Regular Expressions

Since JMeter 2.4, the listener View Results Tree include a RegExp Tester to test regular expressions directly on sampler response data.

There is a Website to test Java Regular expressions.

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.

21. Regular Expressions--from Apache的更多相关文章

  1. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  2. Regular Expressions in Grep Command with 10 Examples --reference

    Regular expressions are used to search and manipulate the text, based on the patterns. Most of the L ...

  3. Introducing Regular Expressions 学习笔记

    Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...

  4. 8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  5. 转载:邮箱正则表达式Comparing E-mail Address Validating Regular Expressions

    Comparing E-mail Address Validating Regular Expressions Updated: 2/3/2012 Summary This page compares ...

  6. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  7. [Regular Expressions] Find Plain Text Patterns

    The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look a ...

  8. [Regular Expressions] Introduction

    var str = "Is this This?"; //var regex = new RegExp("is", "gi"); var r ...

  9. [转]8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

随机推荐

  1. nodejs npm安装教程

    一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资 ...

  2. python接口自动化测试 - requests库的基础使用

    简单介绍 requests库简单易用的HTTP库 Get请求 格式: requests.get(url) 注意:若需要传请求参数,可直接在 url 最后的 ? 后面,也可以调用 get() 时多加一个 ...

  3. imread函数+cvtColor()函数

    加载图像(用cv::imread) imread功能是加载图像文件成为一个Mat对象,其中第一个参数表示图像文件名称 第二个参数,表示加载的图像是什么类型,支持常见的三个参数值 IMREAD_UNCH ...

  4. blog主题——黑夜

    blog主题,存储一下 /* Author: Io_oTI*/ /*Public*/ * { margin: 0; padding: 0; box-sizing: border-box; trans ...

  5. quernation,euler,rotationmatrix之间的相互转换

    转自:https://blog.csdn.net/zhuoyueljl/article/details/70789472

  6. 以C语言为例完成简单的网络聊天程序以及关于socket在Linux下系统调用的分析

    套接字是网络编程中的一种通信机制,是支持TCP/IP的网络通信的基本操作单元,可以看做是不同主机之间的进程进行双向通信的端点,简单的说就是通信的两方的一种约定,用套接字中的相关函数来完成通信过程. 端 ...

  7. numpy-sum函数

    看一个例子就懂了 c = array([[[0, 1, 2, 0, 1, 2]], [[0, 1, 2, 0, 1, 2]]]) print('{0}\n'.format(c.shape)) prin ...

  8. wordpress 不用插件添加友情链接

    哎,也不知道为啥,网上说的那个link manager这个插件死活找不到啊, 找了一个类似的,但是不是,这么多的英文看了好几遍才发现不是 然后从大神哪里找到一个好方法 在你用的那个主题的functio ...

  9. [C/C++] 静态变量赋值问题 undefined reference to

    刚才在写代码的时候 用到了一个静态变量 然后在别人地方直接使用的时候 也就是 NetWork::Flag = 0; 像是这样使用的时候一直提示 undefined reference to 各种检查之 ...

  10. MTSQL主主同步方案

    ** MySQL主主+Keepalived **MySQL+DRBD+Heartbeat 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主主方案,一主多从,读写分离等,但是 ...