0

I have a list of the following strings:

/fajwe/conv_1/routing/apwfe/afjwepfj
/fajwe/conv_2/routing/apwfe
/fajwe/conv_2/routing
/fajwe/conv_3/routing/apwfe/afjwepfj/awef
/fajwe/conv_4/routing/apwfe/afjwepfj/awef/0o09

I want a regex to only match string contains no more than 1 / after the word routing. Namely /fajwe/conv_2/routing/apwfe and /fajwe/conv_2/routing.

Currently I use the regex ^((?!rou\w+(\/\w+){2,}).)*$ but it matches nothing. How can I write a regex to exclude strings contains more than 2 / after the word routing?

I would love to learn how to achieve this using Negative Lookbehind. Many thanks!

spacegoing
  • 4,604
  • 5
  • 22
  • 39

3 Answers3

1

Something like this?

^.*\/routing(\/[^\/]*){0,1}$
gandaliter
  • 9,498
  • 1
  • 13
  • 21
1
routing(\/[^\/]*)?$

there you go

https://regex101.com/r/KjE8ed/1/

bobble bubble
  • 11,625
  • 2
  • 24
  • 38
Schorsch
  • 309
  • 1
  • 14
0

Your regex matches what you are looking for with the multiline flag m as @revo pointed out.

^((?!rou\w+(\/\w+){2,}).)*$

You could also try it like this:

^\/fajwe\/conv_\d\/routing(?:\/[^\/]+)?$

Depending of your context of language you could \/ escape the forward slash

The fourth bird
  • 127,136
  • 16
  • 45
  • 63