import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<![abc]) # No \"a\", \"b\", \"c\" allowed immediately on the left\n"
+ "(?=(?:[bc]*a){3}) # At least three \"a\"s\n"
+ "(?=(?:[ac]*b){3}) # At least three \"b\"s\n"
+ "(?: # Either\n"
+ " (?=[ab]*(?![abc])) # only \"a\" or \"b\"s allowed until a location not followed with \"a\", \"b\" or \"c\"\n"
+ " | # or\n"
+ " (?=(?:[ab]*c){3}) # At least three \"c\"s\n"
+ ")\n"
+ "[abc]+ # Match and consume one or more \"a\", \"b\" or \"c\" chars";
final String string = "Examples for valid sequences:\n\n"
+ "aaabbb\n"
+ "ababab\n"
+ "aaabbbccc\n"
+ "abcabcabc\n"
+ "ababcabcc\n\n"
+ "Examples for invalid sequences (as a whole):\n\n"
+ "aaabbbc\n"
+ "aabbb\n"
+ "abbccc\n"
+ "abcabca";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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