Need help with a problem that has been bothering me for some time!
I am attempting to create an Apache AliasMatch
Regular Expression that will match a URL. The Issue I'm having is that I am using the capture groups as variables $0 $1 $2
within the file match portion. Essentially I need to capture the path/to/controller
portion of the url to actually grab my file and I wish to not use any capture groups after and including the double forward slashes.
http://domain.com/etc/xx/abc/path/to/controller//myDesiredMVCAction
The issue Essentially is that with the following expression:
^/etc/(xx|yy)/(abc|xyz)/(.*)(?=//)(.*)
it only matches
http://domain.com/etc/xx/abc/path/to/controller//myDesiredMVCAction
and not:
http://domain.com/etc/xx/abc/path/to/controller
given that the double slashes and everything trailing it is optional.
So when I make the regex expression optional by appending a ?
it ruins the capture group by including the //myDesiredMVCAction
portion..
^/etc/(xx|yy)/(abc|xyz)/(.*)(?=//)(.*)?
It is possible to achieve what I'm after?
I believe you can use
^/etc/(xx|yy)/(abc|xyz)/(.*?)(?://.*)?$
^^^^^^^^^^^^^^^
See the regex demo
The thing is, the (.*?)(?://.*)?$
part of the pattern works in such a way that (.*?)
is not tried first since it is lazily quantified with *?
, and (?://.*)?$
is tried first, and when the latter does not match, the (.*?)
expands, writing a character to the group value at each expansion step.
The pattern matches:
^/etc/
- /etc/
after base URL(xx|yy)/
- xx
or yy
and then a /
(abc|xyz)/
- an abc
or xyz
and a /
after them(.*?)
- any zero or more characters, but as few as possible before the first(?://.*)?
- (optional group due to ?
at the end) two /
s followed with any characters, as many as possible up to$
- the end of the string input.Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments