Apache2 Regular Expression Optional Capture Group

xRavisher

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?

Wiktor Stribiżew

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular Expression: match everything up to an optional capture group

From Dev

Regular Expression: Treat a group as optional if its not present but if present only capture the previous group before that

From Dev

Regular Expression capture filename and optional extension

From Dev

Regular expression to exclude and capture an optional substring

From Dev

Captured group in optional part of a regular expression

From Dev

Regular Expression to avoid optional group inside an URL

From Dev

Regular Expression Pattern optional group depending on space

From Dev

Regular expression in Vim to match group capture

From Dev

Ruby regular expression non capture group

From Dev

Regular expression stop capture group at ? symbol

From Dev

Regular expression modification to make captured group nullable or optional

From Dev

Regular expression modification to make captured group nullable or optional

From Dev

Why my regular expression matches but does not capture a group?

From Dev

Capture all repetitions of a group using Python regular expression

From Dev

Regular Expression to match the preceding or following alphabetical character in a capture group

From Dev

Python regular expression returning extra capture group for last character matched

From Dev

Capture all repetitions of a group using Python regular expression

From Dev

Regular Expression to match the preceding or following alphabetical character in a capture group

From Dev

What's wrong with the non capture group in my regular expression

From Dev

How to escape the second capture group the first time in javascript regular expression

From Dev

capture group with optional substring

From Dev

regex with optional capture group

From Dev

How to match regular expression group capture collection with another group capture collection?

From Dev

How to match regular expression group capture collection with another group capture collection?

From Dev

optional character in Regular Expression

From Dev

optional character in Regular Expression

From Dev

Replacement with regular expression and capture

From Dev

Capture contents in regular expression

From Dev

Regular expression to capture the digits

Related Related

  1. 1

    Regular Expression: match everything up to an optional capture group

  2. 2

    Regular Expression: Treat a group as optional if its not present but if present only capture the previous group before that

  3. 3

    Regular Expression capture filename and optional extension

  4. 4

    Regular expression to exclude and capture an optional substring

  5. 5

    Captured group in optional part of a regular expression

  6. 6

    Regular Expression to avoid optional group inside an URL

  7. 7

    Regular Expression Pattern optional group depending on space

  8. 8

    Regular expression in Vim to match group capture

  9. 9

    Ruby regular expression non capture group

  10. 10

    Regular expression stop capture group at ? symbol

  11. 11

    Regular expression modification to make captured group nullable or optional

  12. 12

    Regular expression modification to make captured group nullable or optional

  13. 13

    Why my regular expression matches but does not capture a group?

  14. 14

    Capture all repetitions of a group using Python regular expression

  15. 15

    Regular Expression to match the preceding or following alphabetical character in a capture group

  16. 16

    Python regular expression returning extra capture group for last character matched

  17. 17

    Capture all repetitions of a group using Python regular expression

  18. 18

    Regular Expression to match the preceding or following alphabetical character in a capture group

  19. 19

    What's wrong with the non capture group in my regular expression

  20. 20

    How to escape the second capture group the first time in javascript regular expression

  21. 21

    capture group with optional substring

  22. 22

    regex with optional capture group

  23. 23

    How to match regular expression group capture collection with another group capture collection?

  24. 24

    How to match regular expression group capture collection with another group capture collection?

  25. 25

    optional character in Regular Expression

  26. 26

    optional character in Regular Expression

  27. 27

    Replacement with regular expression and capture

  28. 28

    Capture contents in regular expression

  29. 29

    Regular expression to capture the digits

HotTag

Archive