I would like to match url pattern that has optional segments.
I have URL-s like this:
subdomain.domain.com/page/pageurl/pagename/123/
subdomain.domain.com/page/pageurl/pagename/
subdomain.domain.com/page/pageurl/
subdomain.domain.com/page/
Now I have a regex that matches all those situations:
^([a-z]+)\.domain\.com\/page(\/[a-z]+)?(\/[a-z]+)?(\/[0-9]+)?\/?$
But this regexs fails if you go to this URL:
subdomain.domain.com/page/123/
It matches this url too, and I dont want that to happen beacuse first segment should be [a-z]+ and nothing else. Now I do understand why is this happening, but I cant figure out the right regexs to suite my needs.
I need a regexs that would match those URL-s but in order, so if first segment after page is number, it should not match…
How would I do that? Im going crazy right now :S
Rubural example: LINK
Thanks!
We can make the capturing group of the first ‘segment’ mandatory and all of the segments optional like so:
^([a-z]+)\.domain\.com\/page(?:(\/[a-z]+)(\/[a-z]+)?(\/[0-9]+)?)?\/?$Another thing that might be useful is to allow any valid subdomain, the pattern would look like this:
^([\w.-]+)+\.domain\.com\/page(?:(\/[a-z]+)(\/[a-z]+)?(\/[0-9]+)?)?\/?$Edit: Fixed pattern, as Umbrella pointed out (thanks) my prevous pattern would not match your last example string, oops