gpt4 book ai didi

regex - YouTube网址-正则表达式

转载 作者:行者123 更新时间:2023-12-03 05:30:04 25 4
gpt4 key购买 nike

我的反卫星策略文件中有以下配置:

旧的YouTube对象:

<object width="1280" height="720">
<param
name="movie"
value="http://www.youtube.com/v/Hl-zzrqQoSE
?version=3
&amp;hl=en_US
&amp;rel=0">
</param>
<param name="allowFullScreen" value="true">
</param>
<param name="allowscriptaccess" value="always">
</param>
<embed src="http://www.youtube.com/v/Hl-zzrqQoSE
?version=3
&amp;hl=en_US
&amp;rel=0"
type="application/x-shockwave-flash"
width="1280"
height="720"
allowscriptaccess="always"
allowfullscreen="true">
</embed>
</object>

AntiSamy配置:
 <common-regexps>
<regexp name="YouTubeURL" value="(\s)*(http(s?)://)www.youtube.com/v/[\p{L}\p{N}]+[\p{L}\p{N}\p{Zs}\.\#@\$%\+&amp;;:\-_~,\?=/!]*(\s)*"/>
....

<!-- Tags related to YouTube -->
<tag name="object" action="validate">
<attribute name="height"/>
<attribute name="width"/>
<attribute name="type">
<literal-list>
<literal value="application/x-shockwave-flash"/>
</literal-list>
</attribute>
<attribute name="data">
<regexp-list>
<regexp name="YouTubeURL"/>
</regexp-list>
</attribute>
</tag>
<tag name="embed" action="validate">
<attribute name="height"/>
<attribute name="width"/>
<attribute name="type">
<literal-list>
<literal value="application/x-shockwave-flash"/>
</literal-list>
</attribute>
<attribute name="allowfullscreen">
<regexp-list>
<regexp name="boolean"/>
</regexp-list>
</attribute>
<attribute name="allowscriptaccess">
<literal-list>
<literal value="always"/>
</literal-list>
</attribute>
<attribute name="src">
<regexp-list>
<regexp name="YouTubeURL"/>
</regexp-list>
</attribute>
<attribute name="movie">
<regexp-list>
<regexp name="YouTubeURL"/>
</regexp-list>
</attribute>
</tag>

目前,我在iframe上的配置:
    <!-- Frame & related tags -->

<tag name="iframe" action="remove"/>
<tag name="frameset" action="remove"/>
<tag name="frame" action="remove"/>

新的YouTube iframe:
<iframe 
width="1280"
height="720"
<!-- src="https://www.youtube-nocookie.com/embed/Hl-zzrqQoSE" -->
src="https://www.youtube.com/embed/Hl-zzrqQoSE"
frameborder="0"
allowfullscreen>
</iframe>

我认为iframe的代码应如下所示:
<tag name="iframe" action="validate">
<attribute name="height"/>
<attribute name="width"/>
<attribute name="frameborder"/>
<attribute name="src">
<regexp-list>
<regexp name="YouTubeURL"/>
</regexp-list>
</attribute>

<attribute name="allowfullscreen">
<regexp-list>
<regexp name="boolean"/>
</regexp-list>
</attribute>
</tag>

您如何更改正则表达式,使其接受旧的和新的链接,例如:
    https://www.youtube-nocookie.com/embed/Hl-zzrqQoSE
https://www.youtube.com/embed/Hl-zzrqQoSE
https://www.youtube.com/v/Hl-zzrqQoSE
http://www.youtube-nocookie.com/v/Hl-zzrqQoSE?version=3&amp;hl=en_US&amp;rel=0
http://www.youtube.com/v/Hl-zzrqQoSE?version=3&amp;hl=en_US&amp;rel=0"

最佳答案

\s*(https?://)www.youtube(-nocookie)?.com/(?:v|embed)/[\p{L}\p{N}]+[\p{L}\p{N}\p{Zs}.#@$%+&;:_~,?=!/-]*\s*
我自由删除了不必要的捕获组,转义符和角色。
虽然我个人会使用类似的方法:
\s*(https?://www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-]+).*)
这会将整个youtube URL放入匹配组0,将视频ID放入匹配组1。
当youtube的URL不包含unicode字符时,使用unicode属性也不是很有意义。
演示: http://rubular.com/r/jv4zO9ys2L

关于regex - YouTube网址-正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7654542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com