正则表达式在一行中出现多次 [英] Regex multiple occurrences in one line
问题描述
如何使用Java中的正则表达式在一行中查找多个匹配项?
How would I go about finding multiple occurrences in one line using regex in Java?
我的代码(包含正则表达式):
My code (regex included):
public static List<String> getTitles(String html) {
List<String> titles = new ArrayList<String>();
String pattern = "(.*)rel=\"bookmark\">(.*)</a></h2>";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(html);
while (m.find())
System.out.println(m.group(2));
return titles;
}
我正在使用它的部分字符串:
Part of the string I'm using it on:
... title="Permalink to Jet Racing Extreme – Alpha Download" rel="bookmark">Jet Racing Extreme – Alpha Download</a></h2><div class="entry-meta"> Posted on <a ...
这适用于第一次出现(实际上是整个字符串),但由于整个字符串被认为是第一次出现,所以它没有进一步看。
This works for the first occurrence (the whole string actually), but since the whole string is considered the first occurence it's not looking further.
我希望你理解我想说的话,我不知道如何解释它更清晰..
I hope you understand what I'm trying to say, I don't know how to explain it much clearer..
- 编辑
我也尝试用这个替换正则表达式:
--EDIT I've also tried replacing the regex by this:
rel="bookmark">(.*)</a></h2>
我认为这样做只会考虑一行字符串的一部分,从而解决我的问题。然而,仅仅采取以下措施:
I thought doing this would only consider part of the one line string as an occurrence and thus fixing my problem. However, insteading of taking only:
rel="bookmark">Jet Racing Extreme – Alpha Download</a></h2>
出现后,它还需要一切吗?
As the occurence, it takes everything behind it also?
rel="bookmark">Jet Racing Extreme – Alpha Download</a></h2><divclass="entry-meta"> Posted on <ahref="http://www.alphabetagamer.com/jet-racing-extreme-alpha-download/" title="7:29 pm" rel="bookmark"> ...
推荐答案
String pattern = "rel=\"bookmark\">(.*?)</a></h2>";
^^
让他们不贪心。
这篇关于正则表达式在一行中出现多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!