import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = " \"[^\"\\\\]*(?s:\\\\.[^\"\\\\]*)*+\"\n"
+ " (*SKIP)(*F)\n"
+ "|\n"
+ " /\n"
+ " (?:\n"
+ " / .*\n"
+ " |\n"
+ " \\* [^*]* (?: \\*+(?!/) [^*]* )*+ (?:\\*/)?\n"
+ " )";
final String string = "/* toto****to ****/ /****/ \n"
+ "char * foo = \"//don't\\\" // remove\" \" // really please don't\"; // But remove this ha ha! // along with this!\n\n"
+ "char *bad_regex_strategy = \"\\\"good luck\\\" // \\\"counting the \\\"quotes\\\"\" \"//\\\"//\"; // \\\"ha\"\"\" \\\" \" \\\" ha ha\\\" ha /// \"\\;\"ha'\n"
+ "/*\n"
+ "and then imagine a copy of that entire line commented out on the next line of source.\n"
+ "I'd question the entire task and see if there isn't a C source manipulation library you should be using instead. ?\n"
+ " BaseZen 2 hours ago Is it? So when I have the choise between doing this by regex or final state machine, u recommend the FSM, right?\n"
+ " :/ ? user3457394 2 hours ago";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
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