Bonjour,URL extractibles (à tableau) en Ruby
j'apprends sur l'utilisation de RegEx en Ruby, et avoir touché un point où j'ai besoin d'aide. J'essaie d'extraire 0 à plusieurs URL à partir d'une chaîne.
Ce code J'utilise:
sStrings = ["hello world: http://www.google.com", "There is only one url in this string http://yahoo.com . Did you get that?", "The first URL in this string is http://www.bing.com and the second is http://digg.com","This one is more complicated http://is.gd/12345 http://is.gd/4567?q=1", "This string contains no urls"]
sStrings.each do |s|
x = s.scan(/((http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.[\w-]*)?)/ix)
x.each do |url|
puts url
end
end
Voici ce qui est retourné:
http://www.google.com
http
.google
nil
nil
http://yahoo.com
http
nil
nil
nil
http://www.bing.com
http
.bing
nil
nil
http://digg.com
http
nil
nil
nil
http://is.gd/12345
http
nil
/12345
nil
http://is.gd/4567
http
nil
/4567
nil
Quelle est la meilleure façon d'extraire uniquement les URL complètes et non les parties du RegEx?