ruby - regex to detect id parts of a rails url -
ruby - regex to detect id parts of a rails url -
let's have rails url printed out this.
"/admin/corporate_pages/:corporate_page_id/carousel_items/:id/move_down"
how, using regex, 1 substitute parts of url id required, 1. desired output this,
"/admin/corporate_pages/1/carousel_items/1/move_down"
the id appear @ point, musn't count forwards slashes illustration know when substitute. must observe when colon followed string.
i playing around in http://rubular.com/ this. newbie regex, , having read
the road hell paved regular expressions - ilian iliev
i origin sense nauseous.
thanks
regex match 2 :{id_name}
using /(:.+?)\//
url = "/admin/corporate_pages/:corporate_page_id/carousel_items/:id/move_down" url.scan(/(:.+?)\//).flatten =>[":corporate_page_id", ":id"]
explain:
( -- grouping start : -- start : (:) .+? -- char(.) repeat @ to the lowest degree 1 time(+) ungreedy match(?) ) -- grouping end \/ -- stop / (\/)
to replace 2 :{id_name}
'1', need alter regexp /:[^\/]+/
url.gsub(/:[^\/]+/, '1') =>"/admin/corporate_pages/1/carousel_items/1/move_down"
regular look test
ruby-on-rails ruby regex
Comments
Post a Comment