import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<rgb>rgb\\((?<red>\\d{1,3}),\\s*\\d{1,3},\\s*\\d{1,3}\\))$|^(?<rgba>rgba\\(\\d{1,3},\\s*\\d{1,3},\\s*\\d{1,3},\\s*[01](\\.\\d{1,2})?\\))$|^(?<hsla>hsla\\(\\d{1,3},\\s*\\d{1,3}%,\\s*\\d{1,3}%,\\s*[01](\\.\\d{1,2})?\\))$|^(?<hsl>hsl\\(\\d{1,3},\\s*\\d{1,3}%,\\s*\\d{1,3}%\\))$|^(?<hex>#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(\\d{2})?)$|^(?<cmyk>cmyk\\(\\d{1,3}%,\\s*\\d{1,3}%,\\s*\\d{1,3}%,\\s*\\d{1,3}%\\))$";
final String string = "rgb(255,255,255)\n"
+ "rgb(1,23,255)\n"
+ "rgb(132,21,35)\n"
+ "rgba(255,255,255,0.4)\n"
+ "rgba(1,23,255,0.3)\n"
+ "rgba(1,23,255,1)\n"
+ "rgba(1,23,255,1.0)\n"
+ "rgba(132,21,35,0.7)\n"
+ "#403238\n"
+ "#ff323890\n"
+ "#ff890\n"
+ "#FF323890\n"
+ "hsl(240, 100%, 50%)\n"
+ "hsl(248, 53%, 58%)\n"
+ "hsl(147, 50%, 47%)\n"
+ "hsla(9, 100%, 64%, 0.6)\n"
+ "hsla(9, 100%, 64%, 0.8)\n"
+ "hsla(9, 100%, 64%, 0.8)\n"
+ "cmyk(0%, 100%, 100%, 0%)\n"
+ "cmyk(0%,46%,51%,37%)\n\n"
+ "// should fail\n"
+ "rgba(255,255,255)\n"
+ "rgba(1,23,255)\n"
+ "rgba(132,21,35)\n"
+ "rgba(1,23,255,0.)\n"
+ "hsla(240, 100%, 50%)\n"
+ "hsla(248, 53%, 58%)\n"
+ "hsla(147, 50%, 47%)\n"
+ "rgb(255,255,255,0.4)\n"
+ "rgb(1,23,255,0.13)\n"
+ "rgb(132,21,35,0.14)\n"
+ "hsl(9, 100%, 64%, 0.6)\n"
+ "hsl(9, 100%, 64%, 0.8)\n"
+ "hsl(9, 100%, 64%, 0.8)\n"
+ "#FG323890\n"
+ "#ars90\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