简体   繁体   English

为什么jmeter中的正则表达式提取器并不总是提取相同的值?

[英]How come the regular expression extractor in jmeter does not always extract the same value?

I have two WebService(SOAP) Requests. 我有两个WebService(SOAP)请求。 The responses of both web service calls are exactly the same structure-wise. 这两个Web服务调用的响应在结构上完全相同。 For web service call number 1, it has the following structure, 对于网络服务呼叫1,它具有以下结构,

  <data xsi:type="soapenc:string">0</data>
  <data xsi:type="soapenc:string">650</data>

For web service call number 2, it has the following structure, 对于2号Web服务呼叫,它具有以下结构,

  <data xsi:type="soapenc:string">0</data>
  <data xsi:type="soapenc:string">10203</data>

Under each of these calls, I have a regular expression extractor. 在每个调用下,我都有一个正则表达式提取器。 The reference names are different; 参考名称不同; but everything is defined exactly the same. 但所有定义都完全相同。 The extractor has the following definitions. 提取器具有以下定义。

Apply to: Main Sample Only
Response Field to check: Body
Regular Expression: <data xsi:type="soapenc:string">(\d+?)</data>
Template: $1$
Match No.: 0

For the web service call number 1, the extractor gets the value of 650, which is what I want. 对于1号Web服务呼叫,提取程序获得的值是650,这正是我想要的。 But for the web service call number 2, the extractor actually gets the value of 0, which is NOT what I want. 但是对于Web服务调用2,提取器实际上得到的值为0,这不是我想要的。 What I want to get in the web service call number 2 is 10203. My question is, why is it that the regular expression extractor definitions would return two different results? 我想在Web服务调用2中得到的是10203。我的问题是,为什么正则表达式提取器定义将返回两个不同的结果?


Thanks in advance, 提前致谢,
Monte 蒙特

If you're using a match number of zero it will pull a value for your regular expression at random. 如果您使用的匹配数为零,它将为您的正则表达式随机抽取一个值。 0 = Random Match, 1 = first match, 2 = second, etc... 0 =随机匹配,1 =第一场比赛,2 =第二场,依此类推...

Looks like you've asked already same question here . 看来您在这里已经问过同样的问题

  1. Use either Regular Expression Extractor or XPath Extractor - which one is more preferable for parsing xml, as mentioned in comments above, - with the single expression - (for RegEx Extractor: <data xsi:type="soapenc:string">(.+?)</data> , for XPath Extractor: //data/text() - to get ALL the matches. 使用正则表达式提取器XPath提取器 -如上面的注释中所述,更适合解析xml-使用单个表达式-(对于RegEx提取器: <data xsi:type="soapenc:string">(.+?)</data> ,用于XPath提取器: //data/text() -获取所有匹配项。
    Then use corresponding generated variable to access match you need - via ${refName_2} eg (to access 2nd match extracted by query to refName variable), this is actual for both RegEx and XPath Extractors, - or force RegEx Extractor to use specific match by default, via setting "Match No." 然后使用相应的生成变量访问所需的匹配项-通过${refName_2} (例如(将查询提取的第二个匹配项访问refName变量),这对RegEx和XPath提取器都是实际的,或者强制RegEx提取器使用特定的匹配项默认情况下,通过设置“匹配编号” (to 2 as per above example). (根据上述示例为2)。

  2. Use XPath Extractor with xpath query which will uniquely describe required value among the other: by fixed position - like //data[position()=2]/text() in your case (presume that first match is always 0 and you need the 2nd), - or by value - comparing match's text() to 0 like //data[text()!=0]/text() . XPath Extractorxpath查询一起使用 ,它将唯一地描述所需的值:通过固定位置-例如//data[position()=2]/text() (假设您的第一个匹配始终为0,则需要2nd),或按值排序-将匹配的text()与0进行比较,例如//data[text()!=0]/text()

I am puzzled by the ? 我为? in your regular expression: (\\d+?) . 在您的正则表达式中: (\\d+?) This means "one or more digits, but optionally." 这表示“一个或多个数字,但可选”。

Given your input data from call number 1 and call number 2, I would expect it to match the first line in both cases, <data xsi:type="soapenc:string">0</data> , but you say that it doesn't. 给定您来自电话号码1和电话号码2的输入数据,在两种情况下,我希望它与第一行匹配, <data xsi:type="soapenc:string">0</data> ,但是您说它不<data xsi:type="soapenc:string">0</data> “T。

I suspect that don't want to match a number with a leading zero. 我怀疑不想将数字与前导零匹配。 So I would suggest 所以我建议

Regular Expression: <data xsi:type="soapenc:string">([1-9]\d*)</data>

Given web service calls 给定Web服务调用

For web service call number 1, it has the following structure 对于Web服务呼叫1,它具有以下结构

<data xsi:type="soapenc:string">0</data>
  <data xsi:type="soapenc:string">650</data>

For web service call number 2, it has the following structure 对于Web服务呼叫2,它具有以下结构

<data xsi:type="soapenc:string">0</data>
  <data xsi:type="soapenc:string">10203</data>

we can extract the values 650 from the web service call number1 in the following formats 我们可以通过以下格式从Web服务调用number1中提取值650

</data>\s\s<data xsi:type="soapenc:string">(.+)</data>
or
</data>\s\s<data xsi:type="soapenc:string">([0-9]+)</data>

we can extract the values 10203 from the web service call number 2 in the following formats 我们可以通过以下格式从Web服务调用2中提取值10203

</data>\s\s<data xsi:type="soapenc:string">(.+)</data>
or
</data>\s\s<data xsi:type="soapenc:string">([0-9]+)</data>

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

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