1

I have the current url:

ristoranti/location/latvia/riga/other-tag

I need a regexp that do not get the url if has the location segment

Here what I tried:

ristoranti/(?!location$).*)?(.+?)/(.+?)

Example url I need to get:

ristoranti/latvia/riga/other-tag

I'm not so good with regexp but if I'm right the first segment shoulg get all but location, am I wrong?

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
  • Possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Yann Chabot Nov 15 '16 at 15:18
  • You need to remove `/location/` from the string using a simple string operation. Or use two capturing groups, like `(ristoranti/)[^/]*/(.+?)` and replace with `$1$2`. – Wiktor Stribiżew Nov 15 '16 at 15:19
  • I can't remove `/location/` with any operation – Christian Giupponi Nov 15 '16 at 15:21
  • Unclear question. Your regex contains `location`, but the result what you want contains `latvia`. Something went wrong. I suggest to providing some cases to match with the regex and the ouput what you want. – Tân Nov 15 '16 at 15:24

1 Answers1

0

Problem is presence of $ in your negative lookahead that will fail to stop ristoranti/location/latvia/riga/other-tag from matching because your URL is not really ending with location. You should replace it by

(?!location/)

which will fail the match when URL has location/ ahead.

Also use ^ at the start. So your final regex should be:

^ristoranti/(?!location/)([^/]*)/([^/]*)/(.*)

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643