Josh R
Josh R

Reputation: 45

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.

Upvotes: 2

Views: 121

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

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 A-Z 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 A-Z 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

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

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

Upvotes: 1

Related Questions