The extraction and application of jmeter's regular expression

One, extract a single character

analyze data:

{
    "pageSize":20,
    "totalPageNum":1,
    "totalItemNum":1,
    "currentPageNum":1,
    "data":[
        {
            "domain":"testdomainadd0002.com",
            "bookEndTime":"2020-08-25 22:50:00",
            "type":"PreRelease",
            "deleteTime":"2020-08-11",
            "channels":[
                {
                    "id":"91",
                    "name":"GuoYu",
                    "price":1,
                    "transferPrice":69
                }
            ],
            "_map":{

            }
        }
    ],
    "code":"200",
    "msg":"操作成功"
}

The above special characters include the following:

():封装了待返回的匹配字符串

. : 匹配任何字符

? :不要太贪婪,在找到第一个匹配后停止,如果没有?,在找到第一个name后,还会继续往后找,知道找完并输出最后一个数据

Although the above expression can achieve its goal, a more efficient expression is:

 

Where [^"]-means to match anything (except "). In this case, the matching engine will stop searching after it finds the first right ". The matching engine in the above example will look for

Two, extract multiple strings

Suppose I want to extract the value GuoYu of the above name and the value 1.0 of price

Regular expressions that meet the requirements:

"name":"([^"]+)","price":([^"]+)

This will create two combinations and use them as templates for jmeter regular expressions, such as $1$ and $2$

The JMeter regular expression extractor will place the combined value in the specified variable.

Three, keywords

Regular expressions use specific characters as keywords, and these characters have special meaning to the regular expression engine. These characters must be escaped (using a backslash "\") in the string, in order to treat them as primitive characters, not as keywords in regular expressions. Here are the keywords and their meanings

 ^ :^会匹配行或者字符串的起始位置,有时还会匹配整个文档的起始位置。

[abc] : 字符组,匹配包含括号内元素的字符。

 * :   重复零次或更多(贪婪模式)。例如"cccccc" 匹配字符串中所有的c  正则:”c*” 会出到所有的字符”c”。

( ):组合。

[ ]:字符集合。

 {n} :  重复n次。例如从"aaaaaaaa" 匹配字符串的a 并重复3次 正则:"a{3}" 结果就是取到3个a字符 "aaa"。

 {n,} :  重复n次或更多次。与{n,m}不同之处就在于匹配的次数将没有上限,但至少要重复n次 如 正则"a{3,}" a至少要重复3次。

 {n,m} :  重复n到m次。例如正则 "a{3,4}" 将a重复匹配3次或者4次;所以供匹配的字符可以是三个"aaa"也可以是四个"aaaa"正则都可以匹配到。

{n,m}? :  重复n到m次,但尽可能少重复。如 "aaaaaaaa"  正则 "a{0,m}" 因为最少是0次所以取到结果为空

 + :   重复一次或更多次(懒惰模式)。例如"aaaaaaaa" 匹配字符串中所有的a;  正则:"a+"会取到字符中所有的a字符,"a+"与"a*"不同在于"+"至少是一次而"*" 可以是0次。

 ? :   重复零次或一次。例如"aaaaaaaa" 匹配字符串中的a 正则 : "a?" 只会匹配一次,也就是结果只是单个字符a。

*? :   重复任意次,但尽可能少重复。如 "acbacb" 正则"a.*?b" 只会取到第一个"acb" 原本可以全部取到但加了限定符后,只会匹配尽可能少的字符 ,而"acbacb"最少字符的结果就是"acb"。

+? :  重复1次或更多次,但尽可能少重复。

?? :  重复0次或1次,但尽可能少重复。如 "aaacb" 正则 "a.??b"只会取到最后的三个字符"acb"。

.:任意匹配字符。

\:转义字符。

\w :匹配字母,数字,下划线,例如我要匹配”12345BCD__TTz" 正则:"\w+"  这里的"+"字符为一个量词指重复的次数。 

 \s :匹配空格。例如字符 "a b c" 正则:"\w\s\w\s\w"  一个字符后跟一个空格,如有字符间有多个空格直接把"\s" 写成 "\s+" 让空格重复

| -:选择符。

^ $:字符串或行的起始和结尾。

Note: ORO does not support the \Q and \E keywords.

For example: need to extract the brackets in _map, you have to use escaping

 

4. Examples of practical projects

Not much to say, let’s take a look at an example. The example scenario is that we need to obtain the bookable domain name in the reservation list, and then obtain the bookable channel according to one of the domain names.

1. First of all, like me, do a global parameterization of some server parameters. The server.csv file follows the rule of reading by line and separating multiple parameters by commas.

 

2. Establish thread groups separately, and establish HTTP Request, Regular Expression, and View Results Tree. The simple structure looks like this:

3. The parameters returned by the bookable list are a bit complicated: you can use RegExp Tester to debug regular expressions like me.

 

4. After the debugging is correct, you can set the value in the Regular Expression Extractor

Description:

Name of created variable:引用名称,在HTTP等请求中,引用此数据,需要用到的名称

Regular Expression:正则表达式,用于将需要的数据提取出来

Template:模板,表示使用提取到的第几个值:

$-1$:表示取所有值

$0$:表示随机取值

$1$:表示取第1个

$2$:表示取第二个

以此类推:$n$:表示取第n个

Match No:匹配数字,0 代表随机取值,1 代表全部取值

Default Value:缺省值,如果正则表达式没有搜找到值,则使用此缺省值

5. ${domain} can refer to the domain name returned by the reservable list

 

6. Check the result to find that the return is successful

This is an example of a simple application of regular expressions in series correlation interface testing!

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/108082933