import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "'[^']*(?!\\\\)'(*SKIP)(*F) # Make sure we're not matching inside of quotes\n"
+ "|(?m-s:\\s*(?:\\-{2}|\\#)[^\\n]*$) # Single line comment\n"
+ "|(?:\n"
+ " \\/\\*.*?\\*\\/ # Multi-line comment\n"
+ " (?(?=(?m-s:[\\t ]+$)) # Get trailing whitespace if any exists and only if it's the rest of the line\n"
+ " [\\t ]+\n"
+ " )\n"
+ ")";
final String string = "CALL agr_agreement(\n"
+ " 1,\n"
+ " 13075,\n"
+ " 'HBO Latin America--this is a test',\n"
+ " '(123) 456-7890',\n"
+ " 'M',\n"
+ " 'John Doe',\n"
+ " 'john@doecom',\n"
+ " 'Miami',\n"
+ " '(123) 456-7890',\n"
+ " 'Net 30',\n"
+ " 'Quarterly',\n"
+ " 0,\n"
+ " '2014-09-01',\n"
+ " '2014-09-01', /*\n"
+ " This is a multi''-line comment\n"
+ "*/ \n"
+ " '2017-08-31', -- single line comment\n"
+ " 36,\n"
+ " /*Inline, but multi-line comment*/'2015-03-26 06:59:44',\n"
+ " @status\n"
+ " );";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS | Pattern.DOTALL);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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