import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^([a-zA-z]{3})(\\d{4})(?:\\d{2})?(_00)([0-7])(.*)$";
final String string = "HTH0919_001-werelwkre;lkj\n"
+ "HTH0919_007_dgfsdfs\n"
+ "HTH0919_008\n"
+ "HTH0919_009\n"
+ "HtH0919_010\n"
+ "HTH0919_097\n"
+ "HTH0919_098\n"
+ "HTH0919_099\n"
+ "HTH0919_100\n"
+ "HTH0919_111\n"
+ "HTH0919_101\n"
+ "HTH0919_899\n"
+ "HTH123456_020\n\n\n\n"
+ "^([a-zA-z]{3})(\\d{4})(?:\\d{2})?(_00)([0-7])(.*)$\n\n"
+ "^([a-zA-z]{3})(\\d{4})(?:\\d{2})?(_0)(?!98|99)(08|09|[1-9][0-9])(.*)$\n\n"
+ "^([a-zA-z]{3})(\\d{4})(?:\\d{2})?_(098|099|[1-9][0-9][0-9])(.*)$";
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