目录
前言
1. Jmeter正则表达式提取器
2. 入门实例
3. 进阶实例
Jmeter正则表达式提取器属于Jmeter后置处理器(post processors)的一种,用于将取样器请求到的结果以正则表达式的方式读取出来。
1. 作用
JSON提取器只能提取响应内容,而正则表达式提取器可以提取响应头、请求头的值,当然也可以提取响应内容。
2. 参数详解
Name of created variable(将通过正则表达式截取到的值,存到变量中,指定该变量的名称)
当使用正则表达式从一个请求返回信息中,截取多个字段时,此处也可只填写一个变量,如下图所示。
此时,截取到的多个值将会以变量名_g1、变量名_g2来使用,详见下图,这是自己专门实验的截图。
Regular Expression(正则表达式)
可百度在线正则表达式
来练习正则表达式,此处先给出一个在线正则网址。
Template($1$表示会拿到第一个正则表达式匹配到的数据,待确认)
自己的理解,模板Template的含义是提取结果的返回格式模板(当你在Regular Expression(正则表达式)处设置多个提取点时,例如"errmsg":"(.*)","access_token":"(.+?)",那么提取的多个值将以模板Template设置的格式来返回给用户)。
目前自己从其他网商博客了解的信息是,如果在提取器的正则表达式处填写了多个提取点(例如,"errmsg":"(.*)","access_token":"(.+?)"。这对于Jmeter来说,会将此表达式当作两个正则表达式来处理——"errmsg":"(.*)","access_token":和,"access_token":"(.+?)",则可以在Template处填写$1$、$2$等等。$1$代表第一个正则表达式"errmsg":"(.*)","access_token":的截取结果,$2$代表第二个正则表达式,"access_token":"(.+?)"的截取结果,依次类推(注:如果在Template处填写的$1$和$2$间有空格,提取出的(.*)和(.+?)也会以对应空格间隔)。
$0$指全文匹配,$1$ $2$指同时获取第一个和第二个提取点。
Match No.(第几个匹配成功的)
Default Value(当未匹配到时,使用此值返回)
参数不好理解,下面来举例子啦~
3. 正则表达式提取器实栗
还是以这个地址的接口来测试哦
YesApi 小白免费接口 | 果创云,一个账号,搞定接口开发
会员登录接口的响应内容:
{
"ret": 200,
"data": {
"err_code": 0,
"err_msg": "",
"uuid": "F118DD335346CE9D7094C97190587108",
"token": "C1CA708E57F0C33E548745B0D82ADA2A800D886D17BA44261459C39C52376C0D",
"role": "user"
},
"msg": "V3.3.0 YesApi App.User.Login 2022-03-20 10:25:40 hd215"
}
正则表达式提取器-提取token:
正则表达式提取器-提取uuid:
( )
里面写匹配规则,用于解析正则表达式.*?
表示匹配任意长度的任意字符,这也是最常用的正则表达式其他接口调用正则表达式提取的变量:
测试接口:http://api.yesapi.cn/docs-api-App.User.GetList.html
响应内容:
{
"ret":200,
"data":
{
"err_code":0,
"err_msg":"",
"users":
[
{
"uuid":"F118DD335346CE9D7094C97190587108",
"username":"xey032001",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-20 10:25:40",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"AAC71046CFA51B8A55D4367652791D66",
"username":"xey04",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:22:24",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"97F22D1CD99D17C2CF29510C728F6570",
"username":"xey03",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:18:56",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"770A6910AF264AEB73F6ECAFEB5BBFB6",
"username":"xey02",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:15:35",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"690815A62298F01009010BA062773A9F",
"username":"xey01",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:14:03",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"24249E71ABBD72D7C1DE8B3E95A0FBBE",
"username":"xey",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:08:25",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
}
],
"total":6
},
"msg":"V3.3.0 YesApi App.User.GetList 2022-03-20 11:16:58 hd215"
}
1)提取一个值:
正则表达式中只提取一个值,且匹配数字不是-1
1. 未填写模板
PS:匹配数字填1,表示如果有多个uuid,则匹配到第一个,这里与JSON提取器相同
测试结果:
uuid=
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
可以看到,如果正则匹配到值,但是没有填写模板,则返回空
2. 模板写$0$
测试结果:
uuid="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
可以看到,如果模板填写的$0$,uuid取的是uuid_g0
3. 模板写$1$
测试结果:
uuid=F118DD335346CE9D7094C97190587108
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
可以看到,如果模板写$1$,uuid取的是uuid_g1
4. 模板写$2$(不存在)
测试结果:
uuid=null
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
可以看到,如果模板不存在,uuid为null(即使勾选了使用空默认值,也返回null)
2)提取多个值:
测试结果:
uuid_1=F118DD335346CE9D7094C97190587108
uuid_1_g=1
uuid_1_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_1_g1=F118DD335346CE9D7094C97190587108
uuid_2=AAC71046CFA51B8A55D4367652791D66
uuid_2_g=1
uuid_2_g0="uuid":"AAC71046CFA51B8A55D4367652791D66"
uuid_2_g1=AAC71046CFA51B8A55D4367652791D66
uuid_3=97F22D1CD99D17C2CF29510C728F6570
uuid_3_g=1
uuid_3_g0="uuid":"97F22D1CD99D17C2CF29510C728F6570"
uuid_3_g1=97F22D1CD99D17C2CF29510C728F6570
uuid_4=770A6910AF264AEB73F6ECAFEB5BBFB6
uuid_4_g=1
uuid_4_g0="uuid":"770A6910AF264AEB73F6ECAFEB5BBFB6"
uuid_4_g1=770A6910AF264AEB73F6ECAFEB5BBFB6
uuid_5=690815A62298F01009010BA062773A9F
uuid_5_g=1
uuid_5_g0="uuid":"690815A62298F01009010BA062773A9F"
uuid_5_g1=690815A62298F01009010BA062773A9F
uuid_6=24249E71ABBD72D7C1DE8B3E95A0FBBE
uuid_6_g=1
uuid_6_g0="uuid":"24249E71ABBD72D7C1DE8B3E95A0FBBE"
uuid_6_g1=24249E71ABBD72D7C1DE8B3E95A0FBBE
uuid_matchNr=6
一个正则表达式有多个( ):
1. 模板为空:
测试结果:
uuid=
uuid_g=2
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108","username":"xey032001"
uuid_g1=F118DD335346CE9D7094C97190587108
uuid_g2=xey032001
可以看到,如果模板为空,取不到值,uuid为空
2. 只有一个模板:
测试结果:
uuid=F118DD335346CE9D7094C97190587108
uuid_g=2
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108",
"username":"xey032001"
uuid_g1=F118DD335346CE9D7094C97190587108
uuid_g2=xey032001
可以看到,如果只有一个模板,取到了uuid_g1
3. 有两个模板:
测试结果:
uuid=F118DD335346CE9D7094C971q90587108,
xey032001
uuid_g=2
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108",
"username":"xey032001"
uuid_g1=F118DD335346CE9D7094C97190587108
uuid_g2=xey032001
可以看到,uuid取到了两个值uuid_g1和uuid_g2,并且用逗号拼接
注意:
参考文章:
Jmeter正则表达式提取器_jmeter的正则表达式提取器_龙俊亨的博客-CSDN博客
Jmeter基础入门教程【11】--常用功能详解:正则提取器_jmeter正则提取器_短尾巴小鳄鱼的博客-CSDN博客