How to extract URLs from text

前端 未结 6 2113
有刺的猬
有刺的猬 2020-12-03 02:56

How do I extract all URLs from a plain text file in Ruby?

I tried some libraries but they fail in some cases. What\'s the best way?

6条回答
  •  春和景丽
    2020-12-03 03:43

    If you like using what's already provided for you in Ruby:

    require "uri"
    URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
    # => ["http://foo.example.org/bla", "mailto:test@example.com"]
    

    Read more: http://railsapi.com/doc/ruby-v1.8/classes/URI.html#M004495

提交回复
热议问题