3
如何將某些文本轉換爲鏈接?早在PHP中,我用這段代碼,對於我的目的行之有效:使用python中的正則表達式替換URL
$text = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,[email protected]\[\]+]*)(/[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)?)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\3</a>", $text);
$text = preg_replace("#(^|[\n ])(((www|ftp)\.[\w\#$%&~.\-;:=,[email protected]\[\]+]*)(/[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)?)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\3</a>", $text);
我在Python試圖左右,但無法得到它的工作..將是非常好的,如果有人能翻譯這蟒蛇:) ..
什麼沒有工作?你得到了什麼樣的結果?你應該給我們一些關於你嘗試過的東西的暗示。你可能會得到更好的結果,這給了我們一個出發點,尤其是因爲它表明你願意把問題放在工作上。 – 2009-07-10 21:17:38
如果你在Django中做這件事,那就是'urlize'過濾器:http://docs.djangoproject.com/en/dev/ref/templates/builtins/#urlize – Javier 2009-07-10 21:43:36