Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to extract parts of Twitter query

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 =.

like image 648
mac389 Avatar asked Feb 18 '23 11:02

mac389


2 Answers

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
like image 74
Martin Ender Avatar answered Feb 21 '23 01:02

Martin Ender


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.

like image 39
Sufian Latif Avatar answered Feb 21 '23 02:02

Sufian Latif