import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(:61:[0-9]{1,6}[0-9]{4}C[0-9]+,[0-9]{0,2})(NMSC.+)(\\r?\\n:86:RGT FACT[ .])(\\d{5}(?:[. ]\\d{5})*)";
final String string = ":61:2204210421C1339,57NMSCTOPF2510474511//GBBK031SCT TOPF2510474511\n"
+ ":86:RGT FACT 17133 TANQ BROERS SA/RGT39370 TANQ BROERS SA48 AVENUE D'ABCDE\n"
+ ":61:2204270427C4808,37NMSCTOPF2520477320//GBJ6009SCT TOPF2520477320\n"
+ ":86:RGT FACT 17274.17442.17546 TANQ BROERS SA/RGT39370 TANQ BROERS SA48 AVENUE D'ABCDE\n"
+ ":61:2203290329C5518,16NMSCTOPF2485471711//GBCJ001SCT TOPF2485471711\n"
+ ":86:RGT FACT.16794 16918 17079 TANQ BROERS SA/RGT39370 TANQ BROERS SA48 AVENUE D'ABCDE";
final String subst = "$1$2$3INV$4";
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