import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\d+\\[\\d-\\d])\\n";
final String string = "922337203685477580[8-9]\n"
+ "9223372036854775[9-9]\n"
+ "922337203685477[6-9]\n"
+ "92233720368547[8-9]\n"
+ "9223372036854[8-9]\n"
+ "922337203685[5-9]\n"
+ "92233720368[6-9]\n"
+ "9223372036[9-9]\n"
+ "922337203[7-9]\n"
+ "92233720[4-9]\n"
+ "922337[3-9]\n"
+ "92233[8-9]\n"
+ "9223[4-9]\n"
+ "922[4-9]\n"
+ "92[3-9]\n"
+ "9[3-9]";
final String subst = "\\1|";
final Pattern pattern = Pattern.compile(regex, 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