import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^X0\\d+\\s+\\w+\\s*(?P<PARAM_1>\\d+(?:\\.\\d+)?)\\s+(?P<PARAM_2>\\d+(?:\\.\\d+)?)\\s+(?P<PARAM_3>\\d+(?:\\.\\d+)?)";
final String string = "# control.dat\n"
+ "X001 A3 100.0 20.0 40.0\n"
+ " A5 124.0 \n"
+ " I3 125.0 \n"
+ "X002 C3 200.054 20.494 45.0\n"
+ " C5 122.0\n"
+ " K3 122.01\n"
+ " \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