import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "S304JB0...(?<Total1>\\d+).(?<Total2>\\d+).(?<Total3>\\d+).(?<Total4>\\d+).(?<Total5>\\d+).(?<Total6>\\d+).(?<Total7>\\d+)(?:R01(?<Row1>\\d+))?(?:R02(?<Row2>\\d+))?(?:R03(?<Row3>\\d+))?(?:R04(?<Row4>\\d+))?(?:R05(?<Row5>\\d+))?(?:R06(?<Row6>\\d+))?(?:R07(?<Row7>\\d+))?(?:R08(?<Row8>\\d+))?(?:R09(?<Row9>\\d+))?(?:R10(?<Row10>\\d+))?(?:R11(?<Row11>\\d+))?(?:R12(?<Row12>\\d+))?(?:R13(?<Row13>\\d+))?(?:R14(?<Row14>\\d+))?(?:R15(?<Row15>\\d+))?";
final String string = "12*000000000**S304JB01811*8*0*8*4*4*34R0332R152~~~\n"
+ "12*000000000**S304JB01811*9*0*4*3*4*224R023R032R10234R1325~~~\n";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
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