import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([Gg]0?[01]) *(([XxYyZz]) *(-?\\d+.?\\d*)) *(([XxYyZz]) *(-?\\d+.?\\d*))? *(([XxYyZz]) *(-?\\d+.?\\d*))?";
final String string = "G1 X110.406 Y109.680 E6.01498\n"
+ "G1 X110.768 Y109.508 E6.02854\n"
+ "G1 X111.167 Y109.404 E6.04252\n"
+ "G1 X111.596 Y109.379 E6.05707\n"
+ "G1 X112.009 Y109.439 E6.07118\n";
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