6

I've a URL like this url = 'https://www.example.com/contents/6641345'

I want to extract id at the last of url you can say the interger part from the above string

I tried the solution provided https://stackoverflow.com/a/11339230/2391469 but it gives error you can test that answer as well by removing the starting numeric values and putting some where else in the code, that code will throw an error

can anybody help me to get this id?

1 Answer 1

13

You can either split the string

>>> url.split('/')[-1]
'6641345'

Or use a regex

>>> import re
>>> re.findall('\d+', url)
['6641345']

Assuming the urls are always of the format that you showed in your example.

Sign up to request clarification or add additional context in comments.

3 Comments

that's not definitely good to mark every question down, as there are so many new learners in the world, and if this continues(downs) account will be suspended. So please, avoid it...Thanks
@Mubin Was that directed at me? I did not downvote you.
you're the nice one who solved my problem, and those who don't know about this, they focus on to down the questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.