import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "DTS.?(HD|ES|X(?!\\D))|TRUEHD|ATMOS|DD(\\+|P).?([5-9])|EAC3.?([5-9])";
final String string = "#DTS matches\n"
+ "DTSHD\n"
+ "DTSES\n"
+ "DTSX5\n"
+ "DTS5HD\n"
+ "#DTS non-matches\n"
+ "DTSXXX\n"
+ "DTSA\n\n"
+ "#these two are just exact matches\n"
+ "TRUEHD\n"
+ "ATMOS\n"
+ "#they are found anywhere in the text\n"
+ "ATMOSPHERE\n\n"
+ "#DD matches\n"
+ "DD+5\n"
+ "DD+$5 #here $ is matched by .?\n"
+ "DD+45\n"
+ "DDP5\n"
+ "#DD non-matches\n"
+ "DD\n"
+ "DDA\n"
+ "DD+4\n"
+ "DDPx4\n\n"
+ "#EAC matches\n"
+ "EAC345\n"
+ "EAC35\n\n"
+ "#EAC non-matches\n"
+ "EAC34\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