Replace URL with a link using regex in python

Posted by user122750 on Stack Overflow See other posts from Stack Overflow or by user122750
Published on 2009-07-10T21:12:32Z Indexed on 2014/08/20 16:21 UTC
Read the original article Hit count: 195

Filed under:
|
|
|

how do I convert some text to a link? Back in PHP, I used this piece of code that worked well for my purpose:

            $text = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\3</a>", $text);
            $text = preg_replace("#(^|[\n ])(((www|ftp)\.[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\3</a>", $text);

I tried around in Python, but was unable to get it to work.. Would be very nice if someone could translate this to Python :)..

© Stack Overflow or respective owner

Related posts about python

Related posts about regex