0

I have following regex expression

(.*[\/])

This will match /path/tooo/url/

from this string

/path/tooo/url/blablabla-xxxxx

My question is how to modify regex to match

/path/tooo/url 

(without last /)

Thank you.

krist4l
  • 15
  • 1
  • 4

1 Answers1

0

Here's one approach. Match each instance of a forward-slash followed by one or more characters that are not a forward-slash, then match one or more instances of that. Keep matching until we find an instance of a forward-slash followed by only non-forward-slash characters (negative lookahead).

((?:\/[^\/]+)+)(?=\/[^\/]+)

I put a capturing group around the whole thing because it appears you want to capture the result in a capturing group (your regex is wrapped in parens).

I'm using a non-capturing group for the inner regex: (?:) because we don't need to capture this group. See: What is a non-capturing group? What does a question mark followed by a colon (?:) mean?

Community
  • 1
  • 1
granmoe
  • 312
  • 1
  • 11