'Regex for matching url path but without params, first group match

Let's say I have urls like these

  1. https://example.com/link
  2. https://example.com/link?code=1234
  3. https://example.com/link/with/longer/path

What I need is to match accordingly:

  1. link
  2. link
  3. 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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew