🏄‍♀️ Back to index learnings.bolmaster2.com

Regexp on urls with ruby

Published by bolmaster2

In ruby, if you would like to get the components of a URI you would need to use URI that’s not in the core library but in the standard library. Thus making you require it, like this:

1
2
3
4
require 'uri'

uri = URI("https://hello.asdas.com/foo/asdf/asda?aaa=bbb&a=b")
uri.path # "/foo/asdf/asda"

and you could shortcut it without storing it in a variable if you just gonna call URI once:

1
2
require 'uri'
URI("https://hello.asdas.com/foo/asdf/asda?aaa=bbb&a=b").path # "/foo/asdf/asda"

This regexp rather removes the stuff before the path than catching the stuff from the other way, just because I think it’s easier. Here’s how it looks

1
2
url = "https://hello.asdas.com/foo/asdf/asda?aaa=bbb&a=b"
url.gsub(/^http(s)?:\/\/(([a-z]+)|([\.]+))+\//, "") # foo/asdf/asda?aaa=bbb&a=b

A better one, this will capture every part of the url as groups.

1
2
3
4
5
6
7
8
9
10
url = "https://hello.asdas.com/foo/asdf/asda?aaa=bbb&a=b&#fragment"
/^(http[s]?:\/\/)?([^\/]*)([^\?|#]*)([^\#]*)(.*)$/.match(url)
# <MatchData
# "https://hello.asdas.com/foo/asdf/asda?aaa=bbb&a=b&#fragment"
# 1:"https://"
# 2:"hello.asdas.com"
# 3:"/foo/asdf/asda"
# 4:"?aaa=bbb&a=b&"
# 5:"#fragment"
# >

We can do even better with named captures:

1
2
3
4
5
6
7
8
9
10
m = /^(?<scheme>http[s]?:\/\/)?(?<authority>[^\/]*)(?<path>[^\?|#]*)(?<query>[^\#]*)(?<fragment>.*)$/.match(url)

m.named_captures
# {
#   "scheme"=>"https://",
#   "authority"=>"hello.asdas.com",
#   "path"=>"/foo/asdf/asda",
#   "query"=>"?aaa=bbb&a=b&",
#   "fragment"=>"#fragment"
# }

Resources