import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\{.*?\\})|#.*";
final String string = " This is 1. # Comment 1.\n"
+ " This is 2. # Comment 2\n\n"
+ " #Comment 3 {#Commit4}\n"
+ " This is 3.\n\n"
+ " # Commit5\n\n"
+ " This is 4.{#This is 5} # Commit6\n\n"
+ " #Commit7\n\n"
+ " This is 6.\n\n"
+ " # Commit8 #Commit9\n"
+ " #Commit10\n"
+ " # Commit11\n"
+ " #Commit12 #Commit13\n"
+ " { # This is 7}; { # This is 8 } # Commit14\n"
+ " {# This is 9}; { # This is 10 }={# This is 11} {# This is 12}={# This is 13}# Commit15\n"
+ " \n"
+ " # Commit16 {# Commit17 }";
final String subst = "\\1";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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