I am trying to get the name of the resource, I will share with you the
CodePudding user response:
You can use:
^. \/(. )\.. $
^.
- From the start, match as many characters as possible\/
- Match a literal/
.(. )
- Match one or more characters and capture them in a group\.
- Match a literal.
. $
- Match one or more characters at the end of the string (the extension)
Live demo here.
CodePudding user response:
There are many ways:
Couple below using python:
#using regexp:
>>> file_name='https://res-3.cloudinary.com/ngxcoder/image/upload/f_auto,q_auto/v1/blog-images/5oonz9.jpg'
>>> regexpr = r".*/([^\/] ).jpg$"
>>> re.match(regexpr, file_name).group(1)
'5oonz9'
>>>
#to get any file name:
>>> regexpr = r".*/([^\/] )$"
>>> re.match(regexpr, file_name).group(1)
'5oonz9.jpg'
#if interested, here is one using split & take last
>>> (file_name.split("/")[-1]).split(".")[0]
'5oonz9'
>>>
CodePudding user response:
You don't need a capture group, just a match:
(?<=\/)[^\/.] (?=\.[^\/.] $)
We can write the expression in free-spacing mode to make it self-documenting:
(?<= # begin a negative lookbehind
\/ # match '/'
) # end negative lookbehind
[^\] # match one or more characters other than '/'
(?= # begin a positive lookahead
\. # match '.'
[^\/] # match one or more characters other than '/'
$ # match end of string
) # end the positive lookahead
You should not use a regex for this, however, as Python provides os.path
:
import os
str = 'https://res-3.cloudinary.com/ngxcoder/image/'\
'upload/f_auto,q_auto/v1/blog-images/5oonz9.jpg'
base = os.path.basename(str)
print(os.path.splitext(base)[0])
#=> "5oonz9"
Here base #=> "5oonz9.jpg"
.