import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(XXS\\/XS|XS\\/S|S\\/M|M\\/L|L\\/XL|XL\\/XXL|XXS|XS|S|M|L|XL|XXL|\\d{2}\\.\\d{1}|\\d{2}|youth)";
final String string = "XXS/XS - 27.5\"\n"
+ "XS/S - 27.5\"\n"
+ "S/M - 27.5\"\n"
+ "M/L - 27.5\"\n"
+ "M/L - 29\"\n"
+ "L/XL - 27.5\"\n"
+ "L/XL - 29\"\n"
+ "XL/XXL - 29\"\n"
+ "S\n"
+ "M\n"
+ "L\n"
+ "XL\n"
+ "XXL\n"
+ "XXS - 27.5\"\n"
+ "XS - 27.5\"\n"
+ "S - 27.5\"\n"
+ "S - 29\"\n"
+ "M - 27.5\"\n"
+ "M - 29\"\n"
+ "L - 29\"\n"
+ "XL - 29\"\n"
+ "45.5 - 650B\n"
+ "48 - 650B\n"
+ "50.5 - 700c\n"
+ "53 - 700c\n"
+ "55.5 - 700c\n"
+ "58 - 700c\n"
+ "60.5 - 700c\n"
+ "45.5\n"
+ "48\n"
+ "50.5\n"
+ "53\n"
+ "55.5\n"
+ "58\n"
+ "60.5\n"
+ "youth";
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