4

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 :)..

user122750
  • 43
  • 1
  • 3
  • 2
    What didn't work? What kind of results did you get? You should give us a hint about what you tried. You're likely to get better results giving us a starting point, especially since it shows you're willing to put work into the problem. – Blair Conrad Jul 10 '09 at 21:17
  • 2
    if you're doing it in Django, there's the 'urlize' filter: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#urlize – Javier Jul 10 '09 at 21:43
  • Building on Javier's comment, the urlize source seems to be what you want: http://code.djangoproject.com/browser/django/trunk/django/utils/html.py#L77 – hao Jul 11 '09 at 01:20
  • Thanks a lot, that filter (more the urlizetrunc) is really interesting. I should have mentioned that I am using Django. – user122750 Jul 11 '09 at 08:15

1 Answers1

7

The code below is a simple translation to python. You should confirm that it actually does what you want. For more information, please see the Python Regular Expression HOWTO.

import re

pat1 = re.compile(r"(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)

pat2 = re.compile(r"#(^|[\n ])(((www|ftp)\.[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)


urlstr = 'http://www.example.com/foo/bar.html'

urlstr = pat1.sub(r'\1<a href="\2" target="_blank">\3</a>', urlstr)
urlstr = pat2.sub(r'\1<a href="http:/\2" target="_blank">\3</a>', urlstr)

print urlstr

Here's what the output looks like at my end:

<a href="http://www.example.com/foo/bar.html" target="_blank">http://www.example.com</a>
ars
  • 120,335
  • 23
  • 147
  • 134
  • 1
    That's it, it works. I only had to remove that hash-sign pat2 = re.compile(r"# <<--- and in urlstr = pat2.sub(), i used http:// - not just http:/ It looks like I tried it before myself, but I did not know about that "re.IGNORECASE | re.DOTALL" and did not remove the hash-signs that were in my PHP expression. I know I should take a look to some regex book or manual, but in all the years, I only needed regex to do this 'simple' thing, so my motivation is really low. Sorry for that, hope you are not going to kill me :)... – user122750 Jul 11 '09 at 08:20
  • Also, I forgot to mention, that I am doing it this way and not using the filter, because this shows only the name of the domain from the link to the user, not the full URL and not a URL cut down to a given number of chars. – user122750 Jul 11 '09 at 08:24