0

I have data

vk.com/idefiks
vk.com/id211452033
vk.com/id211452033
vk.com/id165402000
vk.com/id_lizalizaelizaveta

I need to get all url, where are numbers after id. If I use

if '/id' in url:

it returns all strings. How can I return only id with number?

  • 2
    Possible duplicate of [Is there a way to substring a string in Python?](http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python) – Ani Menon May 06 '16 at 13:27

4 Answers4

3

you can use re module. for example:

import re
s = """
vk.com/idefiks
vk.com/id211452033
vk.com/id211452033
vk.com/id165402000
vk.com/id_lizalizaelizaveta
"""
p = re.compile('/id\d+')
print p.findall(s)

The output will be:

['/id211452033', '/id211452033', '/id165402000']

PS: if want to remove / in the result, just update regular expression to /(id\d+). that is because, findall just returns the captured groups

Hooting
  • 1,681
  • 11
  • 20
0

Simplest solution :

x = "vk.com/idefiks"
x[6:]

This will give : /idefiks

If you want to omit the / use x[7:].

You may do it using re too, but that's not required for this case.

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
0
if '/id' in url:
    result = url.split('/id')[-1]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
-3
for item in data:
    listItem = item.split('/')
    strId = listItem[1]

try this

moelqudsi
  • 1
  • 1