5

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?

Community
  • 1
  • 1
M K
  • 385
  • 5
  • 14

1 Answers1

11

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    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 – M K Nov 10 '14 at 16:31
  • @Mubin Was that directed at me? I did not downvote you. – Cory Kramer Nov 10 '14 at 16:34
  • you're the nice one who solved my problem, and those who don't know about this, they focus on to down the questions – M K Nov 10 '14 at 16:40