Regular expression to extract parts of a Twitter request

I have the following line from which I want to extract the q and geocode values.

  ?since_id=261042755432763393&q=salvia&geocode=39.862712%2C-75.33958%2C10mi 

I tried the following regex.

 expr = re.compile('\[\=\](.*?)\[\&\]') vals = expr.match(str) 

However, vals is None . I also do not know how to find something earlier, say q= compared to = .

+4
source share
2 answers

No need for 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:

 from urlparse import parse_qs 

and if you used Python before version 2.6, the import statement

 from cgi import parse_qs 
+7
source

I think this can be done easily without a regular expression:

 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.

+1
source

Source: https://habr.com/ru/post/1446795/


All Articles