How to extract URLs from text

前端 未结 6 2118
有刺的猬
有刺的猬 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:34

    You can use regex and .scan()

    string.scan(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/)
    

    You can get started with that regex and adjust it according to your needs.

提交回复
热议问题