import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?P<mounting_type>Z)(?P<number_of_function>2)(?P<unit_type>S)(?P<unit_size>22)(?P<function_in_port>A|B|-)(?P<cracking_pressure>1|2|3|4)(?P<sep_1>-)(?P<unit_series>5X)(?P<sep_2>/|=)(?P<surface_coating>|)(?P<seal_material>V|)(?P<special_version>SO40|SO60|)(?P<additional_details>(.|[0-9])*)$";
final String string = "Z2S22-1-5X=\n"
+ "Z2S22A1-5X/V\n"
+ "Z2S22-1-5X/\n"
+ "Z2S22-2-5X/\n"
+ "Z2S22A1-5X/\n"
+ "Z2S22B1-5X/\n"
+ "Z2S22-4-5X/\n"
+ "Z2S22-1-5X/V\n"
+ "Z2S22B2-5X/\n"
+ "Z2S22-3-5X/\n"
+ "Z2S22A2-5X/\n"
+ "Z2S22-3-5X/V\n"
+ "Z2S22A3-5X/\n"
+ "Z2S22B4-5X/\n"
+ "Z2S22A2-5X/SO40\n"
+ "Z2S22A4-5X/\n"
+ "Z2S22-2-5X/V\n"
+ "Z2S22B3-5X/\n"
+ "Z2S22B1-5X/V\n"
+ "Z2S22B1-5X/SO40\n"
+ "Z2S22A1-5X/SO60\n"
+ "Z2S22B1-5X/SO60\n"
+ "Z2S22A1-5X/SO40\n"
+ "Z2S22A2-5X/V\n"
+ "Z2S22B2-5X/V\n"
+ "Z2S22A4-5X/SO40\n"
+ "Z2S22A3-5X/V\n"
+ "Z2S22B1-5X/VSO60\n"
+ "Z2S22B1-5X/VSO40\n"
+ "Z2S22-4-5X/V\n"
+ "Z2S22B3-5X/SO40\n"
+ "Z2S22A1-5X=SO60\n"
+ "Z2S22B3-5X/V\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