Python regex vimeo id output from url

embed_url = 'http://www.vimeo.com/52422837' response = re.search(r'^(http://)?(www\.)?(vimeo\.com/)?([\/\d+])', embed_url) return response.group(4) 

Answer:

 5 

I was hoping for

 52422837 

Does anyone have an idea? I'm really bad with regular expressions: S

+6
source share
4 answers

Use \d+ (without brackets) according to the literal trait + numbers:

 response = re.search(r'^(http://)?(www\.)?(vimeo\.com/)?(\d+)', embed_url) 

Result:

 >>> re.search(r'^(http://)?(www\.)?(vimeo\.com/)?(\d+)', embed_url).group(4) '52422837' 

You used a group of characters ( [...] ) where it was not necessary. The pattern [\/\d+] matches exactly one of / , + or a digit.

+4
source

Do not reinvent the wheel!

 >>> import urlparse >>> urlparse.urlparse('http://www.vimeo.com/52422837') ParseResult(scheme='http', netloc='www.vimeo.com', path='/52422837', params='', query='', fragment='') >>> urlparse.urlparse('http://www.vimeo.com/52422837').path.lstrip("/") '52422837' 
+10
source

To get everything after the last slash (assuming it is), the following regular expression should do this:

 [^/]*$ 

(Greedily captures everything to the end, which is not a slash).

+1
source

Have you tried to finish your regular expression with a dollar sign ($)?

0
source

All Articles