Home > Enterprise >  Group last segment of a path using RegEx
Group last segment of a path using RegEx

Time:08-27

I have a path where the first segments in this path are constant and will never change, the last segment is variable.

Example:

/my/awesome/path
/my/awesome/path/
/my/awesome/path/1
/my/awesome/path/2

Is it possible using regex to determine the last segment and group it to a certain name?
For example group it to name: id

So the match would be:

1. id=""
2. id=""
3. id="1"
4. id="2"

CodePudding user response:

If you absolutely, 100% must use a regex, here's one possible solution which should work with most regex engines:

^/my/awesome/path/?(.*)$

The first capturing group will contain your id after the known prefix (with our without slash).

  • Related