re = /\/(?<resource>[\w\-]+)(?:\/(?<id>(?!actions)[\w\-]+))?(?:\/actions\/(?<action>[\w\-]+))?/m
str = '/resource
/resource/1
/resource/1/sub-resource
/resource/1/sub-resource/2
/resource/1/sub-resource/2/sub-sub-resource
/resource/1/sub-resource/2/sub-sub-resource/3
/resource/actions/complete
/resource/1/actions/complete
/resource/1/sub-resource/actions/complete
/resource/1/sub-resource/2/actions/complete
/resource/1/sub-resource/2/sub-sub-resource/actions/complete
/resource/1/sub-resource/2/sub-sub-resource/3/actions/complete'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
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