大家好我试图从下面提到的JSON响应中提取令牌(只有令牌值,不需要前缀或后缀)。
常规使用的是:"token":"(.*?)"
但是以下是使用reg ex。
后的返回值"token":"eyJpZCI6IjgzNjg4IiwiZXhwIjoxNDYzMDQ2NDg3LCJzdWIiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImV4dGVybmFsaWQiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsInJvbGVzIjpbIkhJRFN5c0FkbWluIl0sImF1ZCI6InN5c1JNMyIsImlzcyI6Imh0dHBzOlwvXC91c2Jvc3MtdHN0NHRyZXNzOjQ0M1wvYWMuNHRyZXNzLnNhbWxpZHBcL1NJU0RPTUFJTlwvYXV0aG4iLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImlhdCI6MTQ2Mjk2MDA4NywiZ3JvdXBpZHMiOlsiMTAwMCJdfQasddds12"
但我不想要"token":"
和最后一次"
。
请帮我写出符合我标准的正则表达式。
实际JSON响应
{
"statusCode":200,
"result":{
"list":[
{
"provider":"openID",
"token":"eyJpZCI6IjgzNjg4IiwiZXhwIjoxNDYzMDQ2NDg3LCJzdWIiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImV4dGVybmFsaWQiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsInJvbGVzIjpbIkhJRFN5c0FkbWluIl0sImF1ZCI6InN5c1JNMyIsImlzcyI6Imh0dHBzOlwvXC91c2Jvc3MtdHN0NHRyZXNzOjQ0M1wvYWMuNHRyZXNzLnNhbWxpZHBcL1NJU0RPTUFJTlwvYXV0aG4iLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImlhdCI6MTQ2Mjk2MDA4NywiZ3JvdXBpZHMiOlsiMTAwMCJdfQasddds12",
"username":"authNauthor2@adasd.com",
"audience":"sysRM3",
"expires":1463046487,
"roles":[
"asdasd"
],
"groups":[
"1000"
]
},
{
"EMAIL":"authNauthor2@asdasd.com",
"ENDPOINT_ID":null,
"KEY_AUTHORIZATIONS":"IqweQWQCE-001, asdasd-002",
"EU_COMPANY_NAME":"asdasdasd"
}
]
}
}
答案 0 :(得分:1)
查看View Results Tree侦听器的RegExp Tester模式:
您的正则表达式有效,因此您只需在正则表达式提取器中使用$1$
作为模板。完整配置:
token
"token":"(.*?)"
$1$
在必要时将提取的值称为${token}
。
您可以使用Debug Sampler检查提取的变量值:
使用JSON Path Extractor可能更容易通过JMeter Plugins来处理JSON数据,在条件选择方面非常方便,例如“提取令牌,其中email是authNauthor2 @ asdasd。 COM“
相关的JSON路径表达式将像$..token[0]
一样简单。
请参阅:
答案 1 :(得分:0)
我认为您误解了match
和capturing group
之间的区别。让我们稍微减轻你的数据并考虑一下:
"token":"eyJpZCI6IjgzN"
您的模式:"token":"(.*)"
匹配整行,而捕获组将捕获括号内的内容(此处,标记值:{{ 1}})。
您需要获取捕获组的值:
eyJpZCI6IjgzN

此处, var re = /"token":"(.*)"/;
var str = '{ \n "statusCode":200,\n "result":{ \n "list":[ \n { \n "provider":"openID",\n "token":"eyJpZCI6IjgzNjg4IiwiZXhwIjoxNDYzMDQ2NDg3LCJzdWIiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImV4dGVybmFsaWQiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsInJvbGVzIjpbIkhJRFN5c0FkbWluIl0sImF1ZCI6InN5c1JNMyIsImlzcyI6Imh0dHBzOlwvXC91c2Jvc3MtdHN0NHRyZXNzOjQ0M1wvYWMuNHRyZXNzLnNhbWxpZHBcL1NJU0RPTUFJTlwvYXV0aG4iLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImlhdCI6MTQ2Mjk2MDA4NywiZ3JvdXBpZHMiOlsiMTAwMCJdfQasddds12",\n "username":"authNauthor2@adasd.com",\n "audience":"sysRM3",\n "expires":1463046487,\n "roles":[ \n "asdasd"\n ],\n "groups":[ \n "1000"\n ]\n },\n { \n "EMAIL":"authNauthor2@asdasd.com",\n "ENDPOINT_ID":null,\n "KEY_AUTHORIZATIONS":"IqweQWQCE-001, asdasd-002",\n "EU_COMPANY_NAME":"asdasdasd"\n }\n ]\n }\n}';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
document.body.innerHTML += m[1];
}
保持匹配,m[0]
保持捕获组值