码迷,mamicode.com
首页 > 其他好文 > 详细

Regular Expression Extractor (转)

时间:2015-11-23 16:35:07      阅读:383      评论:0      收藏: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

Regular Expression Extractor (转)

标签:

原文地址:http://www.cnblogs.com/Stacie/p/4988445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!