2

I have a table with urls like

vk.com/albums54751623?z=photo54751623_341094858%2Fphotos54751623
vk.com/albums54751623
vk.com/id36375649
vk.com/id36375649

I need to find all urls like vk.com/id36375649 (only id) I try

for url in urls:
    if url == re.compile('vk.com/^[a-z0-9]'):
        print url
    else:
        continue

but this is uncorrectly, because it didn't return anything

1
  • try 'vk\.com\/id\d+' Commented May 5, 2016 at 11:37

2 Answers 2

3

You can use startswith:

strs = ['vk.com/albums54751623?z=photo54751623_341094858%2Fphotos54751623',
'vk.com/albums54751623',
'vk.com/id36375649',
'vk.com/id36375649']
print([x for x in strs if x.startswith(r'vk.com/id')])

See the IDEONE demo

UPDATE

To address the issues stated in comments below this answer, you will have to use a regex with some checks:

^vk\.com/(?!album)\w+$

See the regex demo and a Python demo:

import re
strs = ['vk.com/albums54751623?z=photo54751623_341094858%2Fphotos54751623',
'vk.com/albums54751623',
'vk.com/id36375649',
'vk.com/id36375649',
'vk.com/id36375649?z=album-28413960_228518010',
'vk.com/tania_sevostianova'
]
print([x for x in strs if re.search(r'^vk\.com/(?!album)\w+$', x)])
# => ['vk.com/id36375649', 'vk.com/id36375649', 'vk.com/tania_sevostianova']
Sign up to request clarification or add additional context in comments.

9 Comments

Nice! You don't always need regex :)
I think a regex is not necessary in case there is a good language method for that, and when the data is clean and structured. If that is not the case, sure, a regex like r'vk\.com/id\d+' can be very handy
Some urls like vk.com/id36375649?z=album-28413960_228518010. I don't need that
Do you mean you do not want a vk.com ID link with a query string?
You are changing the question on the fly. You may try a regex then. Try r'^vk\.com/(?!album)\w+$'. See the update.
|
0

A regular expression like the following might work

vk.com\/id\d+

Remember that in regex you need to escape certain characters like slashes.

Comments

Your Answer

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