tags:

views:

39

answers:

2

?valid=true&trans_id=ABC&mpi_status_code=200&acs_url=https://www.secpay.com&MD=123456&PaReq=abc

any one help me write an expression to get the values for acs_url and the MD using regular expressions

Cheers

A: 

I would use:

acs_url=(.*?)&MD=(.*?)&

In group(1) and group(2) you will have searched values.

Key part is ? to finish matching at next char (& in this case)

Michał Niklas
we want to make it future proof and not rely on the MD
Stephen Smithstone
I think regexp is not best way of parsing url. Look at your programming language libraries, there should be something to parse query string. In Python there is `urlparse.parse_qs()`. If you must use regexp and MD part is optional then search for each part separately.
Michał Niklas
A: 

If the parameters are optional and can appear anywhere in the query string, as is normally the case, then you need to use two separate regular expressions:

acs_url=([^&#]) MD=([^&#])

Both regexes have a capturing group to extract the parameter's value.

Though these regular expressions correctly get the values of these parameters, they don't URL-decode them. You'll have to expect things like %2E instead of the . in your acs_url parameter. These regexes may be suitable if you're looking at web logs in a text editor. But if you're writing a script that will actually run on a web server, you should use a class or library designed for processing URL parameters.

Jan Goyvaerts