Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating PHP’s preg_match_all to Python

Tags:

python

regex

php

Can I have a translation of PHP’s preg_match_all('/(https?:\/\/\S+)/', $text, $links) in Python, please? (ie) I need to get the links present in the plain text argument in an array.

like image 321
Amarnath Ravikumar Avatar asked Oct 05 '10 16:10

Amarnath Ravikumar


1 Answers

This will do it:

import re
links = re.findall('(https?://\S+)', text)

If you plan to use this multiple times than you can consider doing this:

import re
link_re = re.compile('(https?://\S+)')
links = link_re.findall(text)
like image 130
Wolph Avatar answered Oct 18 '22 19:10

Wolph