import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?#https://regex101.com/r/eO8pP0/)(?(DEFINE)(?<hex>[a-fA-F\\d])(?<hue>[+-]?(?:\\d{1,8}(?:\\.\\d{0,8})?|\\.\\d{0,8}))(?<percent>(?:\\d{1,2}|100)%)(?<channel>(?&percent)|25[0-5]|2[0-4]\\d|[0-1]\\d\\d|\\d{1,2})(?<rgb>\\h*(?&channel)\\h*(?:,\\h*(?&channel)\\h*){2})(?<hsl>\\h*(?&hue)\\h*(?:,\\h*(?&percent)\\h*){2})(?<alpha>1(?:\\.0*)?|0?\\.\\d{1,8}|0))^(?:#(?:(?&hex){6}|(?&hex){3})|rgb\\h*\\((?&rgb)\\)|hsl\\h*\\((?&hsl)\\)|rgba\\h*\\((?&rgb),\\h*(?&alpha)\\h*\\)|hsla\\h*\\((?&hsl),\\h*(?&alpha)\\h*\\)|inherit|initial|transparent|unset|current[cC]olor|(?# Named colors)aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen)$";
final String string = "red\n"
+ "#f00\n"
+ "#ff0000\n"
+ "rgb(255,0,0)\n"
+ "rgb(100%, 0%, 0%)\n"
+ "hsl(0, 100%, 50%)\n"
+ "hsl(0., 100%, 50%)\n"
+ "hsl(.0, 100%, 50%)\n"
+ "hsl(0.0, 100%, 50%)\n"
+ "rgba(255, 0, 0, 0.5)\n"
+ "hsla(0, 100%, 50%, 0.5)\n"
+ "red\n"
+ "orange\n"
+ "antiquewhite\n"
+ "#0f0\n"
+ "#00ff00\n"
+ "rgba( 34, 12, 64, 0.3)\n"
+ "currentcolor\n"
+ "inherit\n"
+ "initial\n"
+ "unset";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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