URI.extractはこれを行うと主張していますが、一致する括弧を処理しません:
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
かっこで囲まれた URL を壊さずにテキストから URL を抽出する最良の方法 (ユーザーが使用したい方法) は何ですか?
URI.extractはこれを行うと主張していますが、一致する括弧を処理しません:
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
かっこで囲まれた 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 /\(([^\)]*)\)/
この正規表現を使用して、文字列から 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)
ご使用前に
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
これを追加する必要があります
require 'uri'