I have the following string from which I want to extract the q
and geocode
values.
?since_id=261042755432763393&q=salvia&geocode=39.862712%2C-75.33958%2C10mi
I've tried the following regular expression.
expr = re.compile('\[\=\](.*?)\[\&\]')
vals = expr.match(str)
However, vals is None
. I'm also not sure how to find something before, say, q=
versus =
.
No need for a regex (using Python 3):
>>> from urllib.parse import parse_qs
>>> query = parse_qs(str[1:])
>>> query
{'q': ['salvia'], 'geocode': ['39.862712,-75.33958,10mi'], 'since_id': ['261042755432763393']}
>>> query['q']
['salvia']
>>> query['geocode']
['39.862712,-75.33958,10mi']
Obviously, str
contains your input.
Since (according to your tag) you are using Python 2.7, I think you need to change the import statement to this, though:
from urlparse import parse_qs
and if you were using Python before version 2.6, the import statement is
from cgi import parse_qs
I think this can be easily done without regex:
string = '?since_id=261042755432763393&q=salvia&geocode=39.862712%2C-75.33958%2C10mi'
parts = string[1:].split('&') # the [1:] is to leave out the '?'
pairs = {}
for part in parts:
try:
key, value = part.split('=')
pairs[key] = value
except:
pass
And pairs
should contain all the key-value pairs of the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With