Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can multiple trailing slashes can be removed from a URL in Ruby

What I'm trying to achieve here is lets say we have two example URLs:

url1 = "http://emy.dod.com/kaskaa/dkaiad/amaa//////////"
url2 = "http://www.example.com/"

How can I extract the striped down URLs?

url1 = "http://emy.dod.com/kaskaa/dkaiad/amaa"
url2 = "http://http://www.example.com"

URI.parse in Ruby sanitizes certain type of malformed URL but is ineffective in this case.

If we use regex then /^(.*)\/$/ removes a single slash / from url1 and is ineffective for url2.

Is anybody aware of how to handle this type of URL parsing?

The point here is I don't want my system to have http://www.example.com/ and http://www.example.com being treated as two different URLs. And same goes for http://emy.dod.com/kaskaa/dkaiad/amaa//// and http://emy.dod.com/kaskaa/dkaiad/amaa/.

like image 290
splintercell Avatar asked May 04 '10 13:05

splintercell


1 Answers

If you just need to remove all slashes from the end of the url string then you can try the following regex:

"http://emy.dod.com/kaskaa/dkaiad/amaa//////////".sub(/(\/)+$/,'')
"http://www.example.com/".sub(/(\/)+$/,'')

/(\/)+$/ - this regex finds one or more slashes at the end of the string. Then we replace this match with empty string.

Hope this helps.

like image 96
Aliaksei Kliuchnikau Avatar answered Sep 17 '22 09:09

Aliaksei Kliuchnikau