-3

I read all topics about that but there is no solution for me.

I wanna take all data between two line. For ex:

<!-- DETAILS-->

            <li><span>xxxx</span></li>
            <li><span>xxxx</span></li>
            <li><span>xxxx</span></li>
            <li><span>xxxx/span></li>
            <li><span>xxxx</span></li>
            <li><span>xxxx</span></li>
            <li><span>xxxx</span></li>

 <!-- ###DETAILS -->

I tried start end patterns and another regex codes but I can't find solution. My problem I wanna print everything between DETAILS and need your help.

John W.
  • 3
  • 2
  • 3
    you should use an xml/html parser instead of regex: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – krock Feb 25 '17 at 13:15
  • 2
    Please [edit] your question and include all your attempts. This shouldn’t be too hard. – Sebastian Simon Feb 25 '17 at 13:15

1 Answers1

0

You can try this: <!--\s*(\S+)\s*-->((?:\n|.)*)<!--\s*###\1\s*--> and read 2nd capture group. But it will not work for nested tags

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "<!--\\s*(\\S+)\\s*-->((?:\\n|.)*)<!--\\s*###\\1\\s*-->";
final String string = "<!-- DETAILS-->\n\n"
     + "            <li><span>xxxx</span></li>\n"
     + "            <li><span>xxxx</span></li>\n"
     + "            <li><span>xxxx</span></li>\n"
     + "            <li><span>xxxx/span></li>\n"
     + "            <li><span>xxxx</span></li>\n"
     + "            <li><span>xxxx</span></li>\n"
     + "            <li><span>xxxx</span></li>\n\n"
     + " <!-- ###DETAILS -->";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("match: " + matcher.group(1));
}

This will match evrything between <!-- any word --> and <!-- ###any wolrd -->

Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32