我正在尝试使用 RegEx 将响应 header 中位置标记末尾的确认号提取到页面。响应头如下:
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 脚本进行动态页面重定向。任何帮助将不胜感激,如果您需要更多信息来帮助回答问题,请告诉我!
非常感谢。
最佳答案
试试这个:位置:[\S]+?confirmationNumber=(\d+)
您的问题是在字符串中使用特殊字符而不转义它们 - 例如:?
和 /
注意我的 ?
不匹配 confirmationNumber 前面的问号,而是使 [\S]+
非-贪心。
如果你想明确一点,如果像这样修改以转义具有特殊含义的字符,你的版本应该可以工作:
Location: http:\/\/test.regtest.com\/cart\/confirmation\?confirmationNumber=(\d+)?
关于regex - 使用正则表达式从响应头中提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705340/