import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^((?:Final)|(?:\\d{2}:\\d{2})|(?:\\d{1,3}(?!\\d)(?!-)))";
final String string = "PERU\n"
+ "Liga 2\n"
+ "]\n"
+ "Final\n"
+ "Santa Rosa\n\n"
+ "0-3\n\n"
+ "Molinos El Pirata\n\n"
+ "73 \n"
+ "Chavelines\n\n"
+ "1-0\n\n"
+ "Deportivo Coopsol\n\n"
+ "20:30\n"
+ "Comerciantes Unidos\n\n"
+ "-\n\n"
+ "Juan Aurich\n"
+ "22:45\n"
+ "Santos FC\n\n"
+ "-\n\n"
+ "Huaral\n\n"
+ "90+1 \n"
+ "Pogon Szczecin\n\n"
+ "2-0\n\n"
+ "Stal Mielec\n\n"
+ "live\n"
+ "20:30\n"
+ "Plock\n\n"
+ "-\n\n"
+ "Gornik Z.\n\n"
+ "21:15\n"
+ "Farense\n\n"
+ "-\n\n"
+ "Maritimo\n";
final String subst = "newLine$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