Regular Expression Extractor (转)

论坛 期权论坛 脚本     
已经匿名di用户   2022-4-13 16:43   1306   0

Regular Expression Extractor是JMeter中的另一个概念—Post-processor,它的作用是得到HTTP响应后,对结果(比如一个html标签页面的内容或json输出数据等)进行处理,通过regular expression抽取字符值,存成一个变量给后面的Sampler使用。

例如在Click Table(afterSelectTable_ajax.action)这个Sampler执行完一次HTTP Request之后得到如下的HTTP Response(一个json格式的输出):

{"free":false,"hasError":false,"singleEntry":true,"table_nr":"05","tableid":5,"tableuseid":130}

现在要得到tableuseid的值130,可以用这样一个regular expression:

^.+?"tableuseid":([^} ,]+).*$

上面括号中的内容就是提取130的值,用括号的作用是可以把该值作为一个组,并存成一个变量以便在下一个Sampler--CreateOrder(doOpenGuestTable_ajax.action)中使用。

注:可以用下面的perl命令行交互式测试正则表达式:
perl -n -e '/^.+?"tableuseid":([^} ,]+).*$/ && print "$1\n"'

1. 在Regular Expression Extractor会看到Template的值是$1$,这个值是什么意思呢?
$1$是指取第一个()里面的值。如果Regular Expression的数值有多个,用这种方法可以避免不必要的麻烦。

2. Regular Expression中的(.*)是什么意思?
那是一个正则表达式(regular expression)。’.’等同于sql语言中的’?’,表示可有可无。’*’表示0个或多个。’()’表示需要取值。(.*)表达任意长度的字符串。

3. 在读取Regular Expression时要注意什么?
一定要保证所取数值的绝对唯一性。

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

转载于:https://www.cnblogs.com/Stacie/p/4988445.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP