Extracting from Response Header with Regular Expression

user1615559 picture user1615559 · Sep 9, 2013 · Viewed 12.6k times · Source

I'm trying to extract the confirmation number at the end of the location tag in the response header to a page using RegEx. The response header is as follows:

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

For instance, if in the header the line is this:

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

I am looking to return this to use as a variable later:

00284031

My current RegEx expression is something like this:

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

I am new to RegEx and what I wrote above is based off the example at the following link:

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

I need this confirmation number for a dynamic page redirect for a Jmeter script I am writing. Any help would be greatly appreciated and if you require additional information to help answer the question let me know!

Many thanks in advance.

Answer

Matthew picture Matthew · Sep 9, 2013

Try this: Location: [\S]+?confirmationNumber=(\d+)

Your issue is the use of special characters in the string without escaping them - e.g.: ? and /

Note my ? is not matching the question mark in front of confirmationNumber, but instead is making the [\S]+ non-greedy.

If you want to be explicit, your version should work if modified like this to escape the characters with special meaning:

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