0

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 &#8211; Alpha Download" rel="bookmark">Jet Racing Extreme &#8211; 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 &#8211; Alpha Download</a></h2>

As the occurence, it takes everything behind it also?

rel="bookmark">Jet Racing Extreme &#8211; 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"> ...
Jasper Catthoor
  • 473
  • 1
  • 5
  • 22

1 Answers1

4
String pattern = "rel=\"bookmark\">(.*?)</a></h2>";

                                      ^^   

Make them non greedy.

vks
  • 65,133
  • 10
  • 87
  • 119