re = /((?:\/[A-Za-z0-9-]+)+)?(\/tag\/[A-Za-z0-9-%]+)+(\/.*)?/
str = 'http://example.com/cat1/subcat3/subcat4/tag/this%20is%20page/asdasda?start=130/asdasdasd
http://example.com/cat1/subcat3/subcat4/tag/this%20is%20pageasdasd
example.it/news/tag/this%is%20n%page?adsadsadasd
http://example.com/tag/thispage/asdasdasd.-?asds=
http://example.com/tag/this%20is%20page/asdasd
http://example.com/tag/this
'
subst = '$2'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html