Let’s go ahead and write some regex to validate a URI in Ruby:
URI.regexp
That’s it!
We can use it like so
URI.regexp.match(my_uri)
If you have a look at the output of URI.regexp
in irb
, you’ll see the pattern that’s used to match against. It’s relatively complex, but each capture group is documented.
- Scheme
- Opaque (e.g. scheme:foo/bar)
- User Info
- Host
- Port
- Registry
- Path
- Query
- Fragment
irb(main):001:0> URI.regexp.match("http://username:password@foo.bar:80/baz.html?query=string#fragment")
=> #<MatchData "http://username:password@foo.bar:80/baz.html?query=string#frag" 1:"http" 2:nil 3:"username:password" 4:"foo.bar" 5:"80" 6:nil 7:"/baz.html" 8:"query=string" 9:"frag">