简体   繁体   English

正则表达式提取器 Jmeter

[英]Regular Expression Extractor Jmeter

Hi All I am trying to extract the token from the below mentioned JSON Response (only Token value no prefix or suffix is required).大家好我正在尝试从下面提到的 JSON 响应中提取令牌(仅令牌值不需要前缀或后缀)。

The regular ex what is used is: "token":"(.*?)"使用的常规 ex 是: "token":"(.*?)"

but below is the returned value after using the reg ex.但下面是使用 reg ex 后的返回值。

"token":"eyJpZCI6IjgzNjg4IiwiZXhwIjoxNDYzMDQ2NDg3LCJzdWIiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImV4dGVybmFsaWQiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsInJvbGVzIjpbIkhJRFN5c0FkbWluIl0sImF1ZCI6InN5c1JNMyIsImlzcyI6Imh0dHBzOlwvXC91c2Jvc3MtdHN0NHRyZXNzOjQ0M1wvYWMuNHRyZXNzLnNhbWxpZHBcL1NJU0RPTUFJTlwvYXV0aG4iLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhdXRoTmF1dGhvcjJAaGlkZ2xvYmFsLmNvbSIsImlhdCI6MTQ2Mjk2MDA4NywiZ3JvdXBpZHMiOlsiMTAwMCJdfQasddds12"

But I don't want "token":" and the last " to be returned.但我不希望返回"token":"和最后一个"

Please help me in writing the regular expression that meets my criteria.请帮我写出符合我标准的正则表达式。

Actual JSON Response实际的 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"
         }
      ]
   }
}

Looking into RegExp Tester mode of the View Results Tree listener: 查看查看结果树侦听器的 RegExp 测试器模式:

查看结果树

Your regular expression is valid, so you just need to use $1$ as Template in the Regular Expression Extractor.您的正则表达式是有效的,因此您只需要在正则表达式提取器中使用$1$作为模板。 Full configuration:完整配置:

  • Reference Name: anything meaningful, ie token参考名称:任何有意义的东西,即token
  • Regular Expression: "token":"(.*?)"正则表达式: "token":"(.*?)"
  • Template: $1$模板: $1$

Refer extracted value as ${token} where required.在需要时将提取的值引用为${token}

You can check extracted variable value using Debug Sampler :您可以使用Debug Sampler检查提取的变量值:

调试采样器


It might be easier to use JSON Path Extractor available via JMeter Plugins in order to deal with JSON data, it is extremely convenient when it comes to conditional selection like "extract token where email is authNauthor2@asdasd.com"使用通过JMeter Plugins提供的JSON Path Extractor来处理 JSON 数据可能更容易,当涉及到条件选择时非常方​​便,例如“extract token where email is authNauthor2@asdasd.com”

Relevant JSON Path Expression will be as simple as $..token[0] .相关的 JSON 路径表达式将像$..token[0]一样简单。

See:看:

I think you misunderstood the difference between a match and a capturing group .我认为您误解了matchcapturing group之间的区别。 Let's lighten your data a bit and just consider:让我们稍微简化一下您的数据,然后考虑一下:

"token":"eyJpZCI6IjgzN"

Your pattern : "token":"(.*)" will match the whole line while the capturing group will capture the content inside the parenthesis (here, the token value : eyJpZCI6IjgzN ).您的模式: "token":"(.*)"匹配整行,而捕获组将捕获括号内的内容(此处为标记值: eyJpZCI6IjgzN )。

Your need to get the value of the capturing group:您需要获取捕获组的值:

 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]; }

Here, m[0] holds the match while m[1] hold the capturing group value这里, m[0]保存匹配,而m[1]保存捕获组值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM