简体   繁体   English

如何为以下内容提取 JMeter 中的正则表达式?

[英]How can I extract Regular Expression in JMeter for the following?

How can I extract only KK&JK from the following response in JMeter using Regular Expression Extractor?如何使用正则表达式提取器从 JMeter 中的以下响应中仅提取 KK&JK?

<es2:ITEM>C2231597H88-KK&JK-M13122</es2:ITEM>

In the above response C2231597H88 is always 11 characters and M13122 is always 6 characters, but the number of characters for the value KK&JK in this example can change if that helps.在上述响应中,C2231597H88 始终为 11 个字符,M13122 始终为 6 个字符,但如果有帮助,此示例中值 KK&JK 的字符数可以更改。

If I do <es2:ITEM>(.+?)< I get the whole thing C2231597H88-KK&JK-M13122, but I need to capture only KK&JK.如果我这样做<es2:ITEM>(.+?)<我得到了整个 C2231597H88-KK&JK-M13122,但我只需要捕获 KK&JK。

Try the following regex pattern:尝试以下正则表达式模式:

<es2:ITEM>[^-]+-([^-]+)-[^-]+</es2:ITEM>

And then check the first capture group, which should contain what you are trying to target.然后检查第一个捕获组,它应该包含您尝试定位的内容。

Demo演示

If you only want to match KK&JK , another option is to use lookarounds and get a match only:如果您只想匹配KK&JK ,另一种选择是使用环视并仅获得匹配:

(?<=<es2:ITEM>[A-Z0-9]{11}-)[^-<>]+(?=-[A-Z0-9]{6}</es2:ITEM>)

In parts在零件

  • (?<= Positive lookbehind, assert what is directly on the left is not (?<= Positive lookbehind, assert 左边的不是
    • <es2:ITEM>[A-Z0-9]{11}- Match ` and 11 times AZ or 0-9 <es2:ITEM>[A-Z0-9]{11}- Match ` 和 11 次 AZ 或 0-9
  • ) Close lookbehind )近距离观察
  • [^-<>]+ Match 1+ times any char except - < or > [^-<>]+匹配除- <>之外的任何字符 1+ 次
  • (?= Positive lookahead, assert what is directly on the right is (?=正向前瞻,断言右边的内容是
    • -[A-Z0-9]{6}</es2:ITEM> Match 6 times AZ or 0-9 and </es2:ITEM> -[A-Z0-9]{6}</es2:ITEM>匹配 6 次 AZ 或 0-9 和</es2:ITEM>
  • ) Close lookahead )关闭前瞻

Regex demo正则表达式演示

Using 2 capturing groups you might get the value from the second capturing group and use a backreference to group 1 for the closing value:使用 2 个捕获组,您可能会从第二个捕获组中获取值,并使用对第 1 组的反向引用作为结束值:

<(es2:ITEM>)[A-Z0-9]{11}-([^-<>]+)-[A-Z0-9]{6}</\1

In Java double escape the backreference \\1在 Java 中双重转义反向引用\\1

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

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