Home > Blockchain >  How split a URL string containing multiple '&'
How split a URL string containing multiple '&'

Time:05-08

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?

CodePudding user response:

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']
  • Related