import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^\n"
+ "(?> #MAIN iteration (atomic only for efficiency)\n"
+ " (?<upper>[A-Z]) # an uppercase letter\n"
+ " | # or\n"
+ " (?<lower>[a-z]) # a lowercase letter\n"
+ " | # or\n"
+ " (?<digit>[0-9]) # a digit\n"
+ " | # or\n"
+ " (?<special>[^(0-9|a-z|A-Z)]) # a special\n"
+ " | # or\n"
+ " . # anything else\n"
+ "){10,15}? #REPEATED 8+ times\n"
+ " #\n"
+ " #CONDITIONS:\n"
+ "(?(upper) # 1. There must be at least 1 uppercase\n"
+ " (?(lower) # 2. If (1), there must be 1 lowercase\n"
+ " (?(digit) # 3. If (2), there must be 1 digit\n"
+ " (?(special)\n"
+ "# must be 1 special \n"
+ " | (?!) # Else fail\n"
+ " ) #\n"
+ " | (?!) # Else fail\n"
+ " ) #\n"
+ " | (?!) # Else fail\n"
+ " ) #\n"
+ " | (?!) # Else fail\n"
+ ") $ #";
final String string = "1ffdA$@3d3sd$f";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(string);
if (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