简体   繁体   中英

How to capture data between a repeating string in python

I have a string

'request=1417935385131095&to%5B%5D=596254125&to%5B%5D=683330370&e2e=%7B%22submit_0%22%3A1394711375644%7D'

I want the data between "&to%5B%5D=596254125&to%5B%5D=683330370&"

ie I want 596254125 and 683330370.

How can I get it via rejex

I have tried this

 map = re.split(r"&to%5B%5D=\d+&", request)

This is not working. Can you please tell me what I am doing wrong.

Don't use regex when there is a dedicated function to parse url query strings for you:

from urlparse import parse_qs

map = parse_qs(request)
to = map['to[]']

Demo:

>>> from urlparse import parse_qs
>>> request = 'request=1417935385131095&to%5B%5D=596254125&to%5B%5D=683330370&e2e=%7B%22submit_0%22%3A1394711375644%7D'
>>> map = parse_qs(request)
>>> map['to[]']
['596254125', '683330370']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM