import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\<\\!--[\\s\\r\\n-]*[\\w\\r\\n-\\W]*?[\\s\\r\\n-]*--\\>";
final String string = "t('bbbb',a,b,c)\n"
+ "t(\"aaaa\")\n"
+ "t(\"aaaa')\n"
+ "t('aaaa') t('xxxx') // rrrr\n"
+ "t('bbbb',a,1)\n"
+ "// t('bbbb',{x:1,y:\"(,)\",z:[]},1)\n"
+ "t('中\\n国')\n"
+ " //t(\" fdgdfgfd df dfgdfgdf\")\n\n"
+ "<!-- dfdfdf -->\n"
+ "<!---sdsdsd --->\n"
+ "<!--a-->\n"
+ "<!-- 中文 -->\n"
+ "<!--\n"
+ "ewewewe\n"
+ "//-->\n\n"
+ "<!--\n"
+ "ewewewe\n"
+ "//-->\n"
+ "<!--\n"
+ "function displayMsg()\n"
+ "{\n"
+ "alert(\"Hello World!\")\n"
+ "}\n"
+ "//-->\n\n"
+ "fdff\n\n"
+ "/* \n"
+ "fdfdfdfsddssddsdsddsdsdsds\n"
+ "sdsdsd\n"
+ "dsdfsdfsd\n"
+ "fsdfsdfsdfsdf\n"
+ "*/\n\n"
+ "/******dsdsdsdds*/\n"
+ "/*dsdsdsdds*/\n"
+ "/*********/\n\n"
+ "ddd //dddd/*sdds*/\n"
+ " //This is a comment 1\n\n"
+ "let a= 1\n\n"
+ "// This is a comment 2\n"
+ "// This is a comment 3\n"
+ "function test(){\n"
+ " ... //This is a comment 3\n"
+ "}\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html