1

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?

xRavisher
  • 838
  • 1
  • 10
  • 18

1 Answers1

1

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks @Wiktor! Seems like greedy quantification was not analogous to my thought process.. Learnt something new for the day: IQ + 1 ;) – xRavisher Aug 18 '16 at 06:57
  • 2
    How lazy and greedy quantifiers work when used together is difficult to understand. I suggest you take a look at [*Can I improve performance of this regular expression further*](http://stackoverflow.com/questions/33869557/can-i-improve-performance-of-this-regular-expression-further/33869801#33869801). – Wiktor Stribiżew Aug 18 '16 at 07:10