import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = ".*(\\w{4}\\d{7}).*";
final String string = "UNB+UNOA:2+RCW OPS CENTER+TERMINAL+180808:1519+1533741570C3ED+++++RCW OPS CENTER'UNH+01533741570BAP+BAPLIE:D:95B:UN:SMDG22'BGM++CAPSTAN4.20180808151930+9'DTM+137 1808081519UTC:301'TDT+20+081S+++HSD:172:166+++9V7575:103:ZZZ:xxxx xxx'LOC+5+BRSSA:139:6'LOC+61+COCTG:139:6'DTM+178:1808090412 :201'DTM+133:1808091512:201'DTM+132:1808180041:201'RFF+VON:081N'LOC+147+0380412::5'MEA+WT++KGM:29515'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOHAI:139:6'RFF+BM:1'EQD+CN+\n"
+ "SUDU8505087+45G1+++5'NAD+CA+HSD:172:20'LOC+147+0380312::5'MEA+WT++KGM:29586'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOCAU:139:6'RFF+BM:1'EQD+CN+\n"
+ "UACU5363691+45G1+++5'NAD+CA+HLC:172:20'LOC+147+0380212::5'MEA+WT++KGM:29591'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+COCTG:139:6'RFF+BM:1'EQD+CN+\n"
+ "TGHU9702812+45G1+++5'NAD+CA+MSC:172:20'LOC+147+0380112::5'MEA+WT++KGM:29616'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOCAU:139:6'RFF+BM:1'EQD+CN+\n"
+ "HLXU6240079+45G1+++5'NAD+CA+HLC:172:20'LOC+147+0380414::5'MEA+WT++KGM:29476'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+PRSJU:139:6'RFF+BM:1'EQD+CN+\n"
+ "HASU4556735+45G1+++5'NAD+CA+HSD:172:20'LOC+147+0380314::5'MEA+WT++KGM:29476'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOHAI:139:6'RFF+BM:1'EQD+CN+\n"
+ "SUDU6787839+45G1+++5'NAD+CA+HSD:172:20'LOC+147+0380214::5'MEA+WT++KGM:29481'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+COCTG:139:6'RFF+BM:1'EQD+CN+...";
final String subst = "$1\\r\\n";
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