VespaQQ VespaQQ - 2 years ago 88
Python Question

Regex to find links in one row

I have this string:\r

I need to extract all links in one line which ends with \r. It can contain one link or even five links. I got something like this :


but it returns whole row as one match,
any ideas ?

Answer Source

You can use this lookahead based regex in findall:

>>> s='\r'
>>> re.findall(r'https?://.+?(?=https?://|[\r\n]|$)', s)
['', '']

(?=http://|[\r\n]|$) is positive lookahead that asserts next position has http:// or \r or \n or line end.

RegEx Demo

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