import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<type>\\w*)\\/(?<tree>([\\w\\-]+\\.)+)?(?<subtype>[\\w\\-]+)(\\+(?<suffix>[\\w\\-\\.]+))?(; (?<parameter>[\\w+-\\.=]+))?";
final String string = "text/html; charset=UTF-8\n"
+ "application/x-executable\n"
+ "application/graphql\n"
+ "application/javascript\n"
+ "application/json\n"
+ "application/ld+json\n"
+ "application/msword\n"
+ "application/pdf\n"
+ "application/sql\n"
+ "application/vnd.api+json\n"
+ "application/vnd.ms-excel\n"
+ "application/vnd.ms-powerpoint\n"
+ "application/vnd.oasis.opendocument.text\n"
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation\n"
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n"
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document\n"
+ "application/x-www-form-urlencoded\n"
+ "application/xml\n"
+ "application/zip\n"
+ "application/zstd\n"
+ "audio/mpeg\n"
+ "audio/ogg\n"
+ "image/gif\n"
+ "image/apng\n"
+ "image/flif\n"
+ "image/webp\n"
+ "image/x-mng\n"
+ "image/jpeg\n"
+ "image/png\n"
+ "multipart/form-data\n"
+ "text/css\n"
+ "text/csv\n"
+ "text/html\n"
+ "text/php\n"
+ "text/plain\n"
+ "text/xml";
final Pattern pattern = Pattern.compile(regex);
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