Search code examples
regexslashtrailing

regex matching when not trailing slash


How would a regex look like when i have the input

foo.bar/level/1A
foo.bar/level/1A/test
foo.bar/level/1B
foo.bar/level/1A/blabl/sdffs
foo.bar/test

As a result I only want the urls where the root items in the level folder so

foo.bar/level/1A

and

foo.bar/level/1B

So anything in the level folder that is nog followed by a slash basicly What is the best regex way to extract these ?

Thx (I realy need to find a good regex training cause I always bump into this stuff when there is look ahead / back and stuff involved)


Solution

  • You can use:

    ^[^\/]*\/level\/[^\/]*$
    

    Demo: https://regex101.com/r/iqnsjV/1