带有重复字符串的RegEx负向看板。

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

我有一个源字符串,其中有相同的字符串重复多次。 我需要在最后出现的字符串(String A)和另一个字符串(String B)之间进行匹配。 在字符串B之前有多个字符串A的出现。

我一直无法找到一个负向的Lookahead来工作。

向下跳转到修改后的源字符串

源字符串

<a href="Link1">some text 1</a><a href="Link2">some text 2</a><a href="Link3">Required text</a><a href="Link4">some text 4</a>

注:字符串A的数量(<a href=)的发生率会有所不同。

需要的结果

匹配。<a href="Link3">Required text</a>匹配: 与捕获组1。Link 3

使用这个RegEx模式。<a href="(.+?)".+?>Required text<\/a>

匹配: <a href="Link1">some text 1</a><a href="Link2">some text 2</a><a href="Link3">Required text</a> 捕获组1: Link 1

你可以在这里查看设置。 https:/regex101.comrXsEmXw1

我试过很多不同的负面看头,但都没有效果。 例如: <a href="(.+?)"(?!.+?\<a href.+?Required).+?>Required text<\/a>

谢谢你的帮助


修改后的源字符串

好吧,我的错,我在上面的描述中过于简化了Source String。 实际的Source String非常复杂。 你可以在这里查看它。https: /regex101.comrVHrrNj1。

下面提供的答案非常巧妙,但遗憾的是对完整的复杂案例不起作用。<a href="([^"]*)".+?Read more.+?<\/a>

修订后的要求结果

比赛。<a href="https://s2.washingtonpost.com/298378e/5ece9636e6e81b69fea16209/597ba5e59bbc0f6826cfe531/2/10/1db89a19afda3018ce8cfede0cfb6768" style="color: #2a2a2a; font-weight: bold; text-decoration: none;">Read more »</a>

捕获组1。 https://s2.washingtonpost.com/298378e/5ece9636e6e81b69fea16209/597ba5e59bbc0f6826cfe531/2/10/1db89a19afda3018ce8cfede0cfb6768

再次感谢您的帮助

regex regex-lookarounds
1个回答
0
投票

你不需要在这里使用负向查找。你可以使用这个regex来解决这个问题。

<a href="([^"]*)"[^>]*>Read more[^<]*<\/a>
© www.soinside.com 2019 - 2024. All rights reserved.