replace URLs in text with links to URLs

后端 未结 5 1922
逝去的感伤
逝去的感伤 2020-12-16 21:40

Using Python I want to replace all URLs in a body of text with links to those URLs, like what Gmail does. Can this be done in a one liner regular expression?

Edit: b

5条回答
  •  清酒与你
    2020-12-16 22:10

    When you say "body of text" do you mean a plain text file, or body text in an HTML document? If you want the HTML document, you will want to use Beautiful Soup to parse it; then, search through the body text and insert the tags.

    Matching the actual URLs is probably best done with the urlparse module. Full discussion here: How do you validate a URL with a regular expression in Python?

提交回复
热议问题