1

I got a typo in a URI, i.e. colon instead of period. Why URI::regexp pass that kind of URI? For example

"http://www,my.com" =~ /^#{URI::regexp}$/

gives you 0 which means, as far as I know, it's a correct one. But if I try to parse such a URI

URI.parse("http://www,my.com")

it raises URI::InvalidURIError

Why? And how can test a URI properly?

Misha Slyusarev
  • 1,353
  • 2
  • 18
  • 45

1 Answers1

1

From the official documentation about URI.regexp:

Returns a Regexp object which matches to URI-like strings. The Regexp object returned by this method includes arbitrary number of capture group (parentheses). Never rely on its number.

Take a look at Regex to match URL.

Otherwise you can use URI.parse and handle the relevant exception:

begin
  URI::parse("http://www,my.com") # => URI::InvalidURIError
rescue URI::InvalidURIError
  # handle exception
end
Community
  • 1
  • 1
Agis
  • 32,639
  • 3
  • 73
  • 81