[英]Extract URLs from text using Ruby while handling matched parens
URI.extract聲稱可以做到這一點,但它不處理匹配的括號:
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
從文本中提取URL而不破壞帶括號的URL(用戶喜歡使用的URL)的最佳方法是什么?
如果URL總是用括號綁定,則正則表達式可能是更好的解決方案。
text = "text here (http://foo.example.org/bla) and here and here is (http://yet.another.url/with/parens) and some more text"
text.scan /\(([^\)]*)\)/
使用之前
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
您需要添加
require 'uri'
您可以使用此正則表達式從字符串中提取URL
"some thing http://abcd.com/ and http://google.com are great".scan(/(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:(?::[0-9]{1,5})?\/[^\s]*)?/ix)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.