使用正则表达式从响应标头中提取

时间:2013-09-09 19:16:33

标签: regex header jmeter response-headers

我正在尝试使用RegEx将响应标头中位置标记末尾的确认编号提取到页面。响应头如下:

HTTP/1.1 302 Moved Temporarily
Date: Mon, 09 Sep 2013 17:55:50 GMT
Server: Apache-Coyote/1.1
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=00284031
Content-Language: en
Content-Length: 0
X-Cnection: close
Content-Type: text/plain; charset=UTF-8
Vary: Accept-Encoding

例如,如果在标题中该行是:

Location: http://test.regtest.com/cart/confirmation?confirmationNumber=00284031

我希望稍后将其作为变量使用:

  

00284031

我当前的RegEx表达式是这样的:

Location: http://test.regtest.com/cart/confirmation?confirmationNumber=(\d+)?

我是RegEx的新手,我上面写的内容基于以下链接的示例:

http://www.sourcepole.ch/2011/1/4/extracting-text-from-a-page-and-using-it-somewhere-else-in-jmeter

我需要这个确认号,用于我正在编写的Jmeter脚本的动态页面重定向。任何帮助将不胜感激,如果您需要其他信息来帮助回答这个问题,请告诉我们!

非常感谢提前。

3 个答案:

答案 0 :(得分:1)

试试这个:Location: [\S]+?confirmationNumber=(\d+)

您的问题是在字符串中使用特殊字符而不转义它们 - 例如:?/

注意我的?与confirmNumber前面的问号不匹配,而是使[\S]+非贪婪。

如果你想要明确,你的版本应该有效,如果这样修改,以逃避具有特殊含义的字符:

Location: http:\/\/test.regtest.com\/cart\/confirmation\?confirmationNumber=(\d+)?

答案 1 :(得分:1)

您无需匹配整行以获取确认号码,而是可以匹配此号码:

(?<=confirmationNumber=)(\d+)

(?<=confirmationNumber=)被称为背后隐藏,表达式所说的是匹配另一个数字(\d+)并将它们放入一个组中,只有当这些数字前面跟着以下字符串{{1}时}。

Rege101 Demo

答案 2 :(得分:0)

Regexp如下

confirmationNumber=([0-9]+)