Home > Enterprise >  Regular expression to extract the last part without domain
Regular expression to extract the last part without domain

Time:11-22

Good afternoon,

I would like to know how to extract the last part of the path from URL as string, but without domain using Regex from Python style.

The url is: 'https://ncd.soft.com/lags/prime-amazon.png' (prime-amazon is my objective)

I tried with no exit because we need to exclude the domain (.png or .com, etc)

([^/] (?!.png))/?$

Bad result

prime-amazon.png

I expect:

prime-amazon

CodePudding user response:

You can use

[^/] (?=\.png/?$)

See the regex demo.

Details:

  • [^/] - one or more chars other than /
  • (?=\.png/?$) - a positive lookahead that requires .png or .png/ till end of string immediately to the right of the current location.
  • Related