【发布时间】:2015-08-25 08:48:25
【问题描述】:
我正在尝试从单行 html 字符串中检索某些部分。 我写的正则表达式如下:
String pattern = "rel=\"bookmark\">(.*?)</a></h2>"
html 行是这样的:
....rel="bookmark">what I need</a></h2>....rel="bookmark">....rel="bookmark">What I also need</a></h2>....
我的正则表达式有两个结果:
rel="bookmark">what I need</a>
and
rel="bookmark">....rel="bookmark">What I also need</a></h2>
我怎样才能让它对第二个结果不那么贪婪?
【问题讨论】:
-
Related question of yours。看,你有 HTML 并且你正在使用正则表达式。你一个接一个地问。结论:使用 HTML 解析器。 "Regex is not the best tool for that task.".
-
我认为您应该为您的原始问题得到适当的答案。
-
尽管我应该使用 HTML 解析器更好,但我的问题仍然是如何在 Regex 中不那么贪婪。