Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何从HTML中提取youtube id?_Regex_Xpath_Xmlstarlet_Xidel - Fatal编程技术网

Regex 如何从HTML中提取youtube id?

Regex 如何从HTML中提取youtube id?,regex,xpath,xmlstarlet,xidel,Regex,Xpath,Xmlstarlet,Xidel,如何以youtube.com/data-id youtube.com/wXlBep9uFjI格式从该html中提取数据id <div class="video card no_border"> <div class="wrapper" style="background-image: url('https://i.ytimg.com/vi/wXlBep9uFjI/hqdefault.jpg');">

如何以youtube.com/data-id youtube.com/wXlBep9uFjI格式从该html中提取数据id

<div class="video card no_border">
    <div class="wrapper" style="background-image: url('https://i.ytimg.com/vi/wXlBep9uFjI/hqdefault.jpg');">
        <a class="no_click play_trailer" href="/video/play?key=wXlBep9uFjI" data-site="YouTube" data-id="wXlBep9uFjI" data-title="Disney's Artemis Fowl - Teaser Trailer">
            <div class="play_background">
                <span class="glyphicons_v2 play invert svg"></span> 
            </div>
        </a>
    </div>
</div>

这里有一种从Linux终端命令行执行的方法——假设HTML位于一个名为page.HTML的文件中

$sed-rn's/^.*数据id=[^]+.*/youtube.com\/\1/p'page.html youtube.com/wXlBep9uFjI sed命令将数据id值与括号中的模式匹配-如果存在匹配,则括号将创建一个反向引用,该反向引用可以用作替换字符串中的\1,使用查询:

您也可以使用:

或:

输出:
你需要用哪种语言?你确定正则表达式是正确的方法吗?也许你应该试试xPath查询。请问xPath查询是什么?
xmllint --xpath 'string(//a[@data-site="YouTube"]/@data-id)' file.html
xmlstarlet sel -t -v 'string(//a[@data-site="YouTube"]/@data-id)' file.html
xidel -se 'string(//a[@data-site="YouTube"]/@data-id)' file.html
wXlBep9uFjI