Python regex convert youtube url to youtube video

2024/9/19 9:43:09

I'm making a regex so I can find youtube links (can be multiple) in a piece of HTML text posted by an user.

Currently I'm using the following regex to change 'http://www.youtube.com/watch?v=-JyZLS2IhkQ' into displaying the corresponding youtube video:

return re.compile('(http(s|):\/\/|)(www.|)youtube.(com|nl)\/watch\?v\=([a-zA-Z0-9-_=]+)').sub(tag, value)

(where the variable 'tag' is a bit of html so the video works and 'value' a user post)

Now this works.. until the url is like this:

'http://www.youtube.com/watch?v=-JyZLS2IhkQ&feature...'

Now I'm hoping you guys could help me figure how to also match the '&feature...' part so it disappears.

Example HTML:

No replies to this post..Youtube vid:http://www.youtube.com/watch?v=-JyZLS2IhkQMore blabla

Thanks for your thoughts, much appreciated

Stefan

Answer

Here how I'm solving it:

import redef youtube_url_validation(url):youtube_regex = (r'(https?://)?(www\.)?''(youtube|youtu|youtube-nocookie)\.(com|be)/''(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')youtube_regex_match = re.match(youtube_regex, url)if youtube_regex_match:return youtube_regex_matchreturn youtube_regex_match

TESTS:

youtube_urls_test = ['http://www.youtube.com/watch?v=5Y6HSHwhVlY','http://youtu.be/5Y6HSHwhVlY', 'http://www.youtube.com/embed/5Y6HSHwhVlY?rel=0" frameborder="0"','https://www.youtube-nocookie.com/v/5Y6HSHwhVlY?version=3&hl=en_US','http://www.youtube.com/','http://www.youtube.com/?feature=ytca']for url in youtube_urls_test:m = youtube_url_validation(url)if m:print('OK {}'.format(url))print(m.groups())print(m.group(6))else:print('FAIL {}'.format(url))
https://en.xdnf.cn/q/72654.html

Related Q&A

Python / Kivy: conditional design in .kv file

Would an approach similar to the example below be possible in Kivy? The code posted obviously doesnt work, and again its only an example: I will need different layouts to be drawn depending on a certa…

z-axis scaling and limits in a 3-D scatter plot

I performed a Monte Carlo inversion of three parameters, and now Im trying to plot them in a 3-D figure using Matplotlib. One of those parameters (Mo) has a variability of values between 10^15 and 10^2…

How to fix value produced by Random?

I got an issue which is, in my code,anyone can help will be great. this is the example code.from random import * from numpy import * r=array([uniform(-R,R),uniform(-R,R),uniform(-R,R)])def Ft(r):f…

Can I safely assign to `coef_` and other estimated parameters in scikit-learn?

scikit-learn suggests the use of pickle for model persistence. However, they note the limitations of pickle when it comes to different version of scikit-learn or python. (See also this stackoverflow qu…

How to update the filename of a Djangos FileField instance?

Here a simple django model:class SomeModel(models.Model):title = models.CharField(max_length=100)video = models.FileField(upload_to=video)I would like to save any instance so that the videos file name …

CSS Templating system for Django / Python?

Im wondering if there is anything like Djangos HTML templating system, for for CSS.. my searches on this arent turning up anything of use. I am aware of things like SASS and CleverCSS but, as far as I …

How to use chomedriver with a proxy for selenium webdriver?

Our network environment using a proxy server to connect to the outside internet, configured in IE => Internet Options => Connections => LAN Settings, like "10.212.20.11:8080".Now, Im…

django application static files not working in production

static files are not working in production even for the admin page.I have not added any static files.I am having issues with my admin page style.I have followed the below tutorial to create the django …

Celery task in Flask for uploading and resizing images and storing it to Amazon S3

Im trying to create a celery task for uploading and resizing an image before storing it to Amazon S3. But it doesnt work as expected. Without the task everything is working fine. This is the code so fa…

Python logger formatting is not formatting the string

Following are the contents of mylogger.py:def get_logger(name=my_super_logger):log = logging.getLogger(name)log.setLevel(logging.DEBUG)formatter = logging.Formatter(fmt=%(asctime)s %(name)s %(message)s…