Saurav Pathak
Saurav Pathak

Reputation: 836

How do I omit certain parts of a string with python's re?

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

Answers (2)

Rakesh
Rakesh

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

sushanth
sushanth

Reputation: 8302

Here is a solution,

Regex Demo

import re

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

'404.jpg'

Upvotes: 1

Related Questions