0

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

Saurav Pathak
  • 796
  • 11
  • 32

2 Answers2

1

Here is a solution,

Regex Demo

import re

re.sub("/justicefor/(.*)/.*(\.\w+)", r"\1\2", "/justicefor/404/1nirmala5.jpg")

'404.jpg'
sushanth
  • 8,275
  • 3
  • 17
  • 28
1

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
Rakesh
  • 81,458
  • 17
  • 76
  • 113