简体   繁体   English

如何在jmeter中获得常规提取表达式中的最后一个匹配?

[英]How can I get the last match in regular extracor expression in jmeter?

I would like to extract the last occurence of regular expression in Jmeter. 我想在Jmeter中提取正则表达式的最后一次出现。 I used Regular Extractor expression to do this, but I can't got the last occurence. 我使用Regular Extractor表达式执行此操作,但我无法获得最后一次出现。

I tried this : 我试过这个:

  • Regular expression : "var1":([^"]+),"var2" 正则表达式 :“var1”:( [^“] +),”var2“
  • Template : $1$ 模板 :1美元
  • Match No : -1 匹配号 :-1
  • Default value : expression_matchNr 默认值 :expression_matchNr

Then in my script I used ${expression} variable 然后在我的脚本中我使用了$ {expression}变量

I've tested expression_matchNr but it give me the number of match. 我已经测试了expression_matchNr,但它给了我匹配的数量。

What should I put in " Match No: " ? 我应该在“ 匹配号码 ”中加入什么?

Thanks in advance 提前致谢

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 您可以使用JMeter的__V函数来获取expression_3变量的值

As per function's description : 根据功能的描述

For example, if one has variables A1,A2 and N=1: 例如,如果一个变量A1,A2和N = 1:

  • ${A1} - works OK ${A1} - 工作正常
  • ${A${N}} - does not work (nested variable reference) ${A${N}} - 不起作用(嵌套变量引用)
  • ${__V(A${N})} - works OK. ${__V(A${N})} - 工作正常。 A${N} becomes A1, and the __V function returns the value of A1 $ {N}变为A1,__ V函数返回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. 有关函数可以执行哪些操作,请参阅如何使用JMeter函数后续系列。

Unfortunately the JMeter core does not support the -1,-2... for ultimate/penultimate etc. notation. 不幸的是,JMeter核心不支持-1,-2 ......用于终极/倒数第二等符号。 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$ 模板 :$ 2 $

explanation: match anything until (..1..)-happens where it is not possible to match (..1..) after. 解释:匹配任何东西直到(..1 ..) - 发生在之后无法匹配(..1 ..)的地方。 Return (..2..) 返回(.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 至于效率,这会将整个查询[\\ s \\ S] *保存在一个变量中,所以它有点沉重,我还没有测试它是否会占用太多内存如果确实如此,请考虑使用支持javaScript或JQuery的最后的对象方法

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

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