如何使用正则表达式从最后获得最小匹配字符串?

时间:2016-01-15 07:06:54

标签: regex

字符串:

<!-- Page Navigator  -->
<a href="001.html">1</a><a href="002.html">2</a><a href="003.html">3</a><a href="004.html">4</a><a href="005.html">5</a><a href="002.html">next</a>
<!-- Page Navigator  -->

我想得到:

002.html

我写了这个正则表达式,但它没有奏效。 错误的正则表达式:

<a href="(.+?)">next

错误的结果:

001.html">1</a><a href="002.html">2</a><a href="003.html">3</a><a href="004.html">4</a><a href="005.html">5</a><a href="002.html

1 个答案:

答案 0 :(得分:1)

正则表达式从头开始读取字符串,所以

<a href="(.+?)">next
尽管您使用的是&#34;懒惰的量词&#34; +?

,但

仍然是正确的。

您可以改用:

<a href="([^"]+)">next

我在https://regex101.com/

上测试过

[^&#34;]表示任何字符,但不是双引号。

相关问题