-3

I'm trying to retrieve some parts from a single line html string. The Regex I wrote is as follows:

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

The html line is like:

....rel="bookmark">what I need</a></h2>....rel="bookmark">....rel="bookmark">What I also need</a></h2>....

My Regex has two results:

rel="bookmark">what I need</a>
and
rel="bookmark">....rel="bookmark">What I also need</a></h2>

How can I tell it to be less greedy for the second result?

Jasper Catthoor
  • 595
  • 1
  • 6
  • 22
  • [Related question of yours](http://stackoverflow.com/questions/32191573/regex-multiple-occurrences-in-one-line). Look, you have HTML and you are using a regex. And you ask one question after another. Conclusion: Use an HTML parser. ["Regex is not the best tool for that task."](https://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg). – Wiktor Stribiżew Aug 25 '15 at 08:50
  • 1
    I think you should get an appropriate answer for your original question. – Wiktor Stribiżew Aug 25 '15 at 08:55
  • Even though I should be better off with an HTML parser, my question still remains how you would be less greedy in Regex. – Jasper Catthoor Aug 25 '15 at 09:08

1 Answers1

2

Between tags are the things does not contains < and >

String pattern = "rel=\"bookmark\">[^<>]+</a>";
HungPV
  • 489
  • 6
  • 19