How can I get the last match in extracor regex in jmeter?

I would like to extract the last occurrence of a regex in Jmeter. I used the Regular Extractor expression for this, but I cannot get the last event.

I've tried this:

  • Regular expression : "var1": ([^ "] +)," var2 "
  • Template : $ 1 $
  • Match No : -1
  • Default value : expression_matchNr

Then in my script I used the $ {expression} variable

I tested expression_matchNr but it gave me the number of matches.

What should I add to " Match No: "?

Thank you in advance

+3


source to share


2 answers


If you have the following output:

expression=foo
expression_1=foo    
expression_2=bar
expression_3=**what you looking for**
expression_matchNr=3

      

You can use JMeter __V to get the value of a variableexpression_3

According to function description :

For example, if you have variables A1, A2 and N = 1:



  • ${A1}

    - works fine
  • ${A${N}}

    - doesn't work (nested variable reference)
  • ${__V(A${N})}

    - works fine. A $ {N} becomes A1, and the __V function returns A1

So, in your case, what would look like is a function that returns the last match no matter how many matches it looks like:

${__V(expression_${expression_matchNr})}

      

See How to Use JMeter Functions after posting on what else you can do with Functions.

+5


source


Unfortunately, JMeter core doesn't support -1, -2 ... for final / penultimate, etc. notation. However, you can find the end result using negative forward outlook

I stumbled upon this problem and for me I solved it using something like this:

[\s\S]*("var1":([^\"]+),"var2")(?!$1$)

      

and Template : $ 2 $



Explanation: anything before (..1 ..) - happens where it is impossible to match (..1 ..) after. Refund (..2 ..)

Also the first occurrence of the element:

 ("var1":([^\"]+),"var2")(?!$1$)[\s\S]*

      

In terms of efficiency, this stores the entire query [\ s \ S] * in a variable, so it is a bit heavy and I haven't tested it if it is pulling too much memory If so, consider using javaScript or JQuery which supports the latter's methods object

0


source







All Articles