'How split a URL string containing multiple '&'
I'm using Python to split a string so that its results form a list.
String example:
text = 'https://a.com/api?a=1&b=2&c&c=3&https://b.com/api?a=1&b=2&c&c=3'
# It uses '&' to separate multiple URLs. How should I get them out?
# What I need is
URL_list = ['https://a.com/api?a=1&b=2&c&c=3','https://b.com/api?a=1&b=2&c&c=3']
I tried to use str.split() and re.split(), but I didn't get the desired result. Maybe my method is wrong, but what should I do?
Solution 1:[1]
I would use re.split, splitting on an & that is followed by http:// or https:// (checking using a forward lookahead so as not to absorb those characters):
import re
text = 'https://a.com/api?a=1&b=2&c&c=3&https://b.com/api?a=1&b=2&c&c=3'
re.split(r'&(?=https?://)', text)
Output:
['https://a.com/api?a=1&b=2&c&c=3', 'https://b.com/api?a=1&b=2&c&c=3']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nick |
