Search code examples
regexregex-group

Regex - match first group regardless of second group


I'm trying to match all URIs (of some specific pattern) without their suffix. My regex works when the address actually has a suffix, but I want it to also match cases where the suffix is already gone.

Regex:

(.+recordings\/.+)(\/.*)$

Matches:

https://somewebsite.com/recordings/10680-2162-4cff-991a-446ecff1/play
https://somewebsite.com/somethingsomething/recordings/10680-2162-4cff-991a-446ecff1/somesuffix

I'm trying to match this case as well:

https://somewebsite.com/recordings/10680-2162-4cff-991a-446ecff1

I've been playing with this for a couple of hours but unfortunately my knowledge of regex is limited.

you can test my regex here

Regex experts out there please help


Solution

  • You can use

    ^(.*\/recordings\/.*?)(?:\/([^\/]*))?$
    

    See the regex demo

    Details:

    • ^ - start of string
    • (.*\/recordings\/.*?) - Group 1:
      • .* - any zero or more chars other than line break chars as many as possible
      • \/recordings\/ - a literal /recordings/ string
      • .*? - any zero or more chars other than line break chars as few as possible
    • (?:\/([^\/]*))? - an optional non-capturing group matching one or zero occurrences of
      • \/ - a / char
      • ([^\/]*) - Group 2: zero or more chars other than /
    • $ - end of string.