L
o
a
d
i
n
g
.
.
.
https://michele.zonca.org

removing placeholders in url with python + regexp

By Michele Zonca

11 April 2011

Ok, I’m working on the new mashape’s python client library, according to the next big release

Task:

clear an url with placeholder like this one:

http://mashape.com/url?param1=p&param2={value1}&param3={value2}

if “value1” is not in the provided array of parameters, the url should become 

http://mashape.com/url?param1=p&param3={value2}

(later in the process, this placeholders will be substituted with the actual data)

Code:

  keys = re.findall(’{([a-zA-Z0-9-_]*)}’, url)
  for key in keys:
            if key not in parameters:
                re.sub(“[^&?]*={”+key+“}&?”, “”, url)

Hail to the regexp, baby