5

I would like to extract the last occurence of regular expression in Jmeter. I used Regular Extractor expression to do this, but I can't got the last occurence.

I tried this :

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

Then in my script I used ${expression} variable

I've tested expression_matchNr but it give me the number of match.

What should I put in "Match No:" ?

Thanks in advance

Sarroura
  • 553
  • 1
  • 6
  • 15
  • could we get an example plz to better understand what you want ? – dramixx Oct 30 '14 at 08:34
  • You may want to read **whathaveyoutried.com** & show some respect to the StackOverflow Community, which strongly encourages to post high quality questions, altogether with a MCVE ( **a Minimum-Complete-Verifiable-Example of code** ) showing what-you-have-tried so far. You may want to update your post, so as to meet this minimum reasonable level of quality & **to show your will to respect** other StackOverflow contributing members. They are professionals who love to answer good questions on MCVE-related issues. **Enjoy being StackOverflow Member & do support this Community Netiquette** – user3666197 Oct 30 '14 at 09:01
  • 1
    cos rants like that^ make the place so much friendlier.. – CharlieS Oct 31 '14 at 00:42

2 Answers2

6

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's __V function to get value of expression_3 variable

As per function's description:

For example, if one has variables A1,A2 and N=1:

  • ${A1} - works OK
  • ${A${N}} - does not work (nested variable reference)
  • ${__V(A${N})} - works OK. A${N} becomes A1, and the __V function returns the value of A1

So in your case the function, returning last match no matter how many matches are will look like:

${__V(expression_${expression_matchNr})}

See How to Use JMeter Functions post series on what else can be done with functions.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Unfortunately the JMeter core does not support the -1,-2... for ultimate/penultimate etc. notation. You can however find the ultimate occurrence using a negative look-ahead

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

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

and Template: $2$

explanation: match anything until (..1..)-happens where it is not possible to match (..1..) after. Return (..2..)

Also the first occurrence of the item:

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

As for efficiency, this saves the entire query [\s\S]* in a variable, so it is a bit heavy and I haven't tested if it pulls too much memory If it does, consider using javaScript or JQuery which does support last object methods