import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([A-Z0-9]{4})";
final String string = " ; Message Number\n"
+ " ; | Time Offset (ms)\n"
+ " ; | | Type\n"
+ " ; | | | ID (hex)\n"
+ " ; | | | | Data Length\n"
+ " ; | | | | | Data Bytes (hex) ...\n"
+ " ; | | | | | |\n"
+ " ;---+-- ----+---- --+-- ----+--- + -+ -- -- -- -- -- -- --\n"
+ " 1) 2.0 Rx 0400 8 01 5A 01 57 01 D2 A6 02 \n"
+ " 2) 8.6 Rx 0500 8 02 C1 02 C9 02 BE 02 C2 \n"
+ " 3) 36.2 Rx 0401 8 01 58 01 59 01 01 01 01 \n"
+ " 4) 41.7 Rx 01C4 8 27 9C 64 8C 00 03 E8 08 \n"
+ " 5) 43.1 Rx 0501 8 02 C0 02 C1 02 C6 02 C0 \n"
+ " 6) 62.7 Rx 01C2 8 27 9C 60 90 00 0F 04 08 ";
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