Home > Blockchain >  Regex to take path until slash ("/") or end of string
Regex to take path until slash ("/") or end of string

Time:11-05

It is quite easy using lookarounds but they not supported in Lucene/Elasticsearch hence I have troubles to get category/vegetables/<colour> from:

category/vegetables/green
category/vegetables/green/avocado
category/vegetables/green/kiwi
category/vegetables/yellow

We can assume that category/vegetables is a static part - I just need to have a regexp that matches category/vegetables/green and category/vegetables/yellow but not others.

I was trying: category\/vegetables\/.*?~(\/) - take anything if it has not a backslash but it doesn't work.

CodePudding user response:

It seems you wanted to use .*?~(\/) as an equivalent of (?:(?!\/).)*?, which is a very inefficient way to write a pattern like [^\/]*? (although [^\/] matches line breaks whereas . does not match line break chars, and, unfortunately, its behavior cannot be redefined in Lucene regex flavor).

Since you do not want to allow slashes in the part after category/vegetables/, you can simply use

category/vegetables/[^/] 

Note that Lucene regexes are automatically anchored, so this pattern will require the whole string to match.

  • Related