我有一个链接列表(在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>
,我只想抓取技术规格和下载链接。没有更多,没有更少。出于这个原因,我编写了这个regex /<a href="(.*)">[\s\S]*(Download|Tech Specs)[\s\S]*<\/a>/mgUu
,但不幸的是,它也捕获了下载的保修链接。我如何才能改变我的模式,以排除这一点?我知道这和一些负面的表情有关,但我想不出.啊,在$matches数组中,除了链接,我还需要匹配的文本在捕获组中,这样我就知道哪个是链接,哪个是.蒂娅。
发布于 2022-11-23 23:59:32
参见本演示:https://regex101.com/r/wztpJQ/1
它使用这个regex (?<=<a href=")(?P<link>[^"]*)(?=" .*>\n.*\n\t*(?P<name>.*Specs|.*Download)\n.*<\/a>)
。
只有当a标记以其前面的特定文本结尾时,它才匹配href值,注意到是如何根据</a>
标记之前的最后一个单词进行匹配的。
注释2演示程序有组名
https://stackoverflow.com/questions/74554089
复制相似问题