Preg匹配两个短语中的任何一个,但不匹配第三个

问题描述 投票:0回答:2

我有一个链接列表(在

a
中也有一些 svg 图标 - 它使我的模式更加复杂,这就是我提到这一点的原因),并且我想获取两个特定的链接。

因此,如果这是要搜索的主题:

            <h2>title</h2>      
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.      
        
            <a href="#" role="button">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V274.7l-73.4-73.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l128 128c12.5 12.5 32.8 12.5 45.3 0l128-128c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 274.7V32zM64 352c-35.3 0-64 28.7-64 64v32c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V416c0-35.3-28.7-64-64-64H346.5l-45.3 45.3c-25 25-65.5 25-90.5 0L165.5 352H64zM432 456c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg>      
                        Download the warranty
                    </a>
   
<a href="#" role="button">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V274.7l-73.4-73.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l128 128c12.5 12.5 32.8 12.5 45.3 0l128-128c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 274.7V32zM64 352c-35.3 0-64 28.7-64 64v32c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V416c0-35.3-28.7-64-64-64H346.5l-45.3 45.3c-25 25-65.5 25-90.5 0L165.5 352H64zM432 456c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg>      
                        Tech Specs
                    </a>



<a href="#" role="button">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V274.7l-73.4-73.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l128 128c12.5 12.5 32.8 12.5 45.3 0l128-128c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 274.7V32zM64 352c-35.3 0-64 28.7-64 64v32c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V416c0-35.3-28.7-64-64-64H346.5l-45.3 45.3c-25 25-65.5 25-90.5 0L165.5 352H64zM432 456c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg>      
                        Download
                    </a>

,我只想获取技术规格下载链接。不多不少。因此,我编写了这个正则表达式

/<a href="(.*)">[\s\S]*(Download|Tech Specs)[\s\S]*<\/a>/mgUu
,但不幸的是,它也捕获了 下载保修 链接。我怎样才能改变我的模式以排除这种情况?我知道这与一些负面的环视有关,但我无法弄清楚...啊,在 $matches 数组中,除了链接之外,我还需要匹配的文本位于捕获组中,这样我知道哪个链接是哪个……TIA。

https://regex101.com/r/cvXzkS/1

php regex preg-match-all
2个回答
0
投票

更可靠和自记录的方法是使用 DomDocument 和 Xpath 的组合来解析 HTML 并提取合格的值。

在 xpath 查询中,定位所有已修剪文本值且与两个指定值相匹配的

a
标签。

循环限定节点并将 href 和文本值存储到结果数组中。

代码:(演示

$dom = new DOMDocument();
libxml_use_internal_errors(true); // so that the svg tags don't make noise
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//a[normalize-space(.)='Tech Specs' or normalize-space(.)='Download']");

$result = [];
foreach ($nodes as $node) {
    $result[] = [$node->getAttribute('href'), trim($node->nodeValue)];
}
var_export($result);

-1
投票

请参阅此演示:https://regex101.com/r/wztpJQ/1

它使用这个正则表达式

(?<=<a href=")(?P<link>[^"]*)(?=" .*>\n.*\n\t*(?P<name>.*Specs|.*Download)\n.*<\/a>)

仅当 a 标签以特定文本结尾时,它才匹配 href 值,注意它如何根据

</a>
标签之前的最后一个单词进行匹配

注2演示有组名

© www.soinside.com 2019 - 2024. All rights reserved.