McLeodx McLeodx - 1 year ago 72
Python Question

How to find substrings in a string that end when they reach a character pattern (Python)

I have one long string which is dozens of urls. Is there a non-regex way to turn this into a list of urls, starting with the pattern

and ending each substring when
is found a second time?</a>

It's one long string without breaks.

Answer Source

Just try this:

" http".join(url.split("http")).split()

>>> url = "</a>"
>>> " http".join(url.split("http")).split()
['', '', '', '', '', '', '', '</a>', '']

Essentially this just adds a space before every "http" then splits by " ".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download