Let's say I have urls like these
https://example.com/link
https://example.com/link?code=1234
https://example.com/link/with/longer/path
What I need is to match accordingly:
link
link
link/with/longer/path
This regex: ^(?:[^/]*(?:/(?:/[^/]*/?)?)?([^?] )(?:\??. )?)$
is able to group a path, but because of a tool I use, I need to match preciesliy just that one group - this regex matches whole URLs.
Can I make it match just the first group? Or maybe there is something smarter to do it?
CodePudding user response:
You can use
(?<=(?<!/)/)(?!/)[^?]
See the regex demo. Details:
(?<=(?<!/)/)
- a positive lookbehind that fails the match if there is a/
char immediately to the left of the current location that is not immediately preceded with another/
char (the check if performed with a(?<!/)
negative lookbehind)(?!/)
- a negative lookahead that fails the match if there is a/
char immediately to the right of the current location[^?]
- one or more chars other than a?
char.