之前,我解释如下,因为我创建了一个问题save regex permalink not work well。
所以,如果你想测试它,你可以手动尝试。
我在http://www.pyregex.com 上检查你的正则表达式模式;
1.模式:findall
2. 模式:
(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})
3. 测试字符串:
https://youtu.be/yVpbFMhOAwE
https://www.youtube.com/watch?v=8Z5EjAmZS1o
https://www.youtube.com/embed/yVpbFMhOAwE
<iframe width="560" height="315" src="https://www.youtube.com/embed/Tlf00NT6mig" frameborder="0" allowfullscreen></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/KQzCxdO3hvo" frameborder="0" allowfullscreen></iframe>
https://www.youtube.com/watch?v=VslLZcV9ZcU&list=RD8Z5EjAmZS1o&index=2
4.结果:
["https://","","youtu","be","","yVpbFMhOAwE"]
["https://","www.","youtube","com","watch?v=","8Z5EjAmZS1o"]
["https://","www.","youtube","com","embed/","yVpbFMhOAwE"]
["https://","www.","youtube","com","embed/","Tlf00NT6mig"]
["https://","www.","youtube","com","embed/","KQzCxdO3hvo"]
["https://","www.","youtube","com","watch?v=","VslLZcV9ZcU"]
我认为您应该这样做以获取视频 ID;
def get_youtube_urls(self):
youtube_regex = (
r'(https?://)?(www\.)?'
'(youtube|youtu|youtube-nocookie)\.(com|be)/'
'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
matches = re.findall(youtube_regex, self.text)
urls = []
for url in matches:
id = url[-1] # from last index of `matches` list.
if len(id) == 11: # check if `id` is valid or not.
urls.append(id)
return urls
如果不行,建议你把上面youtube_regex的值改成这个(不保存在元组里)。
youtube_regex = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
# OR, recomended to use this;
youtube_regex = r'http(?:s?):\/\/(?:www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
更新
为了得到正确的位置,我建议你使用finditer;
>>> youtube_regex = r'http(?:s?):\/\/(?:www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
>>> pattern = re.compile(youtube_regex)
>>> [{'origin': m.group(), 'id': m.group(4), 'position': m.start()} for m in pattern.finditer(test_strings) ]
[
{'position': 0, 'id': 'yVpbFMhOAwE', 'origin': 'https://youtu.be/yVpbFMhOAwE'},
{'position': 30, 'id': '8Z5EjAmZS1o', 'origin': 'https://www.youtube.com/watch?v=8Z5EjAmZS1o'},
{'position': 75, 'id': 'yVpbFMhOAwE', 'origin': 'https://www.youtube.com/embed/yVpbFMhOAwE'},
{'position': 156, 'id': 'Tlf00NT6mig', 'origin': 'https://www.youtube.com/embed/Tlf00NT6mig'},
{'position': 280, 'id': 'KQzCxdO3hvo', 'origin': 'https://www.youtube.com/embed/KQzCxdO3hvo'},
{'position': 366, 'id': 'VslLZcV9ZcU', 'origin': 'https://www.youtube.com/watch?v=VslLZcV9ZcU'}
]
>>
然后,用模板标签处理它。下面这个脚本还没有完成,你需要把<iframe等标签去掉,或者从原文评论中去掉..
import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
def iframe_video(id):
"""
return html string iframe video from the `id`.
"""
return '<iframe src="https://www.youtube.com/embed/{}"></iframe>'.format(id)
@register.filter
def safe_comment(text_comment):
"""
{{ comment.text|safe_comment|linebreaks|urlize }}
"""
youtube_regex = (r'http(?:s?):\/\/(?:www\.)?'
'(youtube|youtu|youtube-nocookie)\.(com|be)/'
'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
pattern = re.compile(youtube_regex)
matches = [
{'origin': m.group(), 'id': m.group(4), 'position': m.start()}
for m in pattern.finditer(text_comment)
]
for match in matches:
id = match['id']
origin = match['origin']
position = match['position']
# text_comment.replace(origin, iframe_video(id))
# do something to replace tag <iframe, or else..
return mark_safe(text_comment)