简体   繁体   中英

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?

<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.

If I do <es2:ITEM>(.+?)< I get the whole thing C2231597H88-KK&JK-M13122, but I need to capture only 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:

(?<=<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
    • <es2:ITEM>[A-Z0-9]{11}- Match ` and 11 times AZ or 0-9
  • ) Close lookbehind
  • [^-<>]+ Match 1+ times any char except - < or >
  • (?= 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>
  • ) 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:

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

In Java double escape the backreference \\1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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