3

我正在尝试使用 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 脚本进行动态页面重定向。任何帮助将不胜感激,如果您需要其他信息来帮助回答问题,请告诉我!

提前谢谢了。

4

3 回答 3

1

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

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

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

如果您想明确,如果像这样修改您的版本以转义具有特殊含义的字符,您的版本应该可以工作:

Location: http:\/\/test.regtest.com\/cart\/confirmation\?confirmationNumber=(\d+)?
于 2013-09-09T19:22:44.800 回答
1

您不需要匹配整行来获得确认号码,而是可以像这样匹配号码:

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

(?<=confirmationNumber=)被称为向后看,表达式所说的是匹配更多数字(\d+)并将它们放入一个组中,前提是这些数字前面有以下字符串confirmationNumber=

Rege101 演示

于 2013-09-09T19:38:34.793 回答
0

正则表达式如下

confirmationNumber=([0-9]+)
于 2014-11-27T13:21:00.267 回答