سؤال

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.

هل كانت مفيدة؟

المحلول

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+)?

نصائح أخرى

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

Regexp as following

confirmationNumber=([0-9]+)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top