import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=SUBH2002078568)[\\s\\S]+?(?=SUBF2002078568)";
final String string = "HMT940040324\n"
+ "SUBH2002078568\n"
+ "2002078568{1:F01BANK MBI}{2:I940MAP}{4:\n"
+ "2002078568:20:20210420182417\n"
+ "2002078568:25:2002078568\n"
+ "2002078568:28C:00075\n"
+ "2002078568:60F:D210420IDR0,\n"
+ "2002078568:62F:D210420IDR0,\n"
+ "2002078568-}\n"
+ "SUBF2002078568\n"
+ "SUBH2003001298\n"
+ "2003001298{1:F01BANK MBI}{2:I940MAP}{4:\n"
+ "2003001298:20:20210420182417\n"
+ "2003001298:25:2003001298\n"
+ "2003001298:28C:00075\n"
+ "2003001298:60F:C210420IDR111520964,38\n"
+ "2003001298:62F:C210420IDR111520964,38\n"
+ "2003001298-}\n"
+ "SUBF2003001298\n"
+ "SUBH2002078568 // *Added this account from the top*\n"
+ "2002078568{1:F01BANK MBI}{2:I940MAP}{4:\n"
+ "2002078568:20:20210420182417\n"
+ "2002078568:25:2002078568\n"
+ "2002078568:28C:00075\n"
+ "2002078568:60F:D210420IDR0,\n"
+ "2002078568:62F:D210420IDR0,\n"
+ "2002078568-}\n"
+ "SUBF2002078568- // End\n"
+ "FMT9400000004";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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