Reputation: 836
I have this string:
url = '/justicefor/404/1nirmala5.jpg'
I want to extract it as 404.jpg
.
I tried something like:
pattern = re.compile(
r"./justicefor/(\d+/.\.\w+)",
re.IGNORECASE
)
But this selects the text between 404 and jpg too. How do I fix this?
I'm new to regular expressions so
Upvotes: 0
Views: 42
Reputation: 82775
You can use the os
module
Ex:
import os
url = '/justicefor/404/1nirmala5.jpg'
path, ext = os.path.splitext(url)
print(os.path.basename(os.path.dirname(path)) + ext) #--> 404.jpg
Upvotes: 1
Reputation: 8302
Here is a solution,
import re
re.sub("/justicefor/(.*)/.*(\.\w+)", r"\1\2", "/justicefor/404/1nirmala5.jpg")
'404.jpg'
Upvotes: 1