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.
Regex experts out there please help
CodePudding user response:
You could change the . at the end to \S to match non whitespace chars ad then optionally match the last part matching non whitespace chars without /
(\S recordings\/[^\s\/] )(?:\/.*)?$
Or match the protocol and optional non whitespace chars
^(https?:\/\/\S*\/recordings\/[^\s\/] )(?:\/.*)?$
CodePudding user response:
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.
