Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting from Response Header with Regular Expression

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.

like image 506
user1615559 Avatar asked Sep 16 '26 18:09

user1615559


2 Answers

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+)?
like image 156
Matthew Avatar answered Sep 18 '26 11:09

Matthew


You don't need to match the entire line to get the confirmation number, instead you can just match the number like this:

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

(?<=confirmationNumber=) is called a look behind, what the expression says is to match one more digits (\d+) and put them into a group, only if those digits are preceded by the following string confirmationNumber=.

Rege101 Demo

like image 36
Ibrahim Najjar Avatar answered Sep 18 '26 10:09

Ibrahim Najjar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!