-1

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"
Dirk
  • 308
  • 2
  • 11
  • Do you really need regex for this? Couldn't you strip the known prefix(es) and have your answer? e.g. pseudo-code `id = url.stripLeft("/my/awesome/path").stripLeft("/")` – knittl Aug 26 '22 at 11:00
  • Unfortunately I do. Because this is just the base for a specific use case to fulfill. – Dirk Aug 26 '22 at 11:02
  • But why does this use-case _require_ a regex? Why can't it be fulfilled through other means? – knittl Aug 26 '22 at 11:05
  • This is just a very simple example of the real use-case. My hope is to avoid big if/else constructs in my code to realize this. Maybe RegEx can do this job for me in an efficient way. – Dirk Aug 26 '22 at 11:09
  • In that case, please give an example that is closer to your real use case. Right now, I don't see how a RegEx would simplify or is even required. Also, no big if/else constructs are required (as can seen by the pseudo code). You might even achieve your result by splitting the string, removing the first N parts and then read your id. https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ – knittl Aug 26 '22 at 11:10
  • Also, you haven't mentioned which regex dialect you can use (or in which programming language your regex engine is implemented) – knittl Aug 26 '22 at 11:12
  • Let's suppose we have a hash-table. The key in this table can look like "/my/path/:id", where :id represent the variable part. If now any pathname comes in, f.e. "/my/path/1" I want to verify whether this path matches to the key in my hash-table and which value contains the variable segment of my path. Maybe RegEx isn't the best tool to achieve this. I don't know. – Dirk Aug 26 '22 at 11:21
  • It looks like you are trying to perform path matching similar to how the [AntPathMatcher](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html) works. I'm pretty sure there exist similar implementations for C++. – knittl Aug 26 '22 at 11:44

1 Answers1

1

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).

knittl
  • 246,190
  • 53
  • 318
  • 364
  • This looks good, but can I give the first group a name? – Dirk Aug 26 '22 at 11:24
  • What do you mean? The first group is called `1`. Naming groups looks different, depending on the regex engine and some do not support it at all. – knittl Aug 26 '22 at 11:28
  • In C++ the ECMAScript is used. Giving the capturing group a name is just a gold frame and not really necessary. Thank you for your effort. – Dirk Aug 26 '22 at 11:33
  • @Dirk [Named capturing groups are not supported by C++ 11](https://stackoverflow.com/a/16888480/112968) – knittl Aug 26 '22 at 11:45
  • Not supported? That was unexpected. tyvm – Dirk Aug 26 '22 at 11:48