Sathy
Sathy

Reputation: 303

python regex on variable

Please help with my regex problem

Here is my string

source="http://www.amazon.com/ref=s9_hps_bw_g200_t2?pf_rd_m=ATVPDKIKX0DER&pf_rd_i=3421"
source_resource="pf_rd_m=ATVPDKIKX0DER"

The source_resource is in the source may end with & or with .[for example].

So far,

regex = re.compile("pf_rd_m=ATVPDKIKX0DER+[&.]")
regex.findall(source)
[u'pf_rd_m=ATVPDKIKX0DER&']

I have used the text here. Rather using text, how can i use source_resource variable with & or . to find this out.

Upvotes: 0

Views: 85

Answers (3)

icecrime
icecrime

Reputation: 76745

If the goal is to extract the pf_rd_m value (which it apparently is as you are using regex.findall), than I'm not sure regex are the easiest solution here:

>>> import urlparse
>>> qs = urlparse.urlparse(source).query
>>> urlparse.parse_qs(qs)
{'pf_rd_m': ['ATVPDKIKX0DER'], 'pf_rd_i': ['3421']}
>>> urlparse.parse_qs(qs)['pf_rd_m']
['ATVPDKIKX0DER']

Upvotes: 3

BergmannF
BergmannF

Reputation: 10235

You can just build the string for the regular expression like a normal string, utilizing all string-formatting options available in Python:

import re
source_and="http://rads.stackoverflow.com/amzn/click/B0030DI8NA/pf_rd_m=ATVPDKIKX0DER&"
source_dot="http://rads.stackoverflow.com/amzn/click/B0030DI8NA/pf_rd_m=ATVPDKIKX0DER."
source_resource="pf_rd_m=ATVPDKIKX0DER"
regex_string = source_resource + "[&\.]"
regex = re.compile(regex_string)
print regex.findall(source_and)
print regex.findall(source_dot)
>>> ['pf_rd_m=ATVPDKIKX0DER&']
['pf_rd_m=ATVPDKIKX0DER.']

I hope this is what you mean.

Just take note that I modified your regular expression: the . is a special symbol and needs to be escaped, as is the + (I just assumed the string will only occur once, which makes the use of + unnecessary).

Upvotes: 1

E.Z.
E.Z.

Reputation: 6661

You also have to escape the .

pattern=re.compile(source_resource + '[&\.]')

Upvotes: 2

Related Questions