import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:(?:rgb\\((\\d{1,3}),\\s?(\\d{1,3}),\\s?(\\d{1,3})\\))|(?:rgba\\((\\d{1,3}),\\s?(\\d{1,3}),\\s?(\\d{1,3}),\\s?([0-1]\\.0|0\\.[0-9])\\)))";
final String string = "rgb(0, 0, 0)\n"
+ "rgb(0,0,0)\n"
+ "rgb(255, 255, 255)\n"
+ "rgba(255, 255, 255, 1)\n"
+ "rgba(255, 255, 255,1)\n"
+ "rgba(255, 255, 255, 0)\n"
+ "rgba(255, 255, 255, 0.9)\n"
+ "rgba(255, 255, 255, 0.0)\n"
+ "rgba(255, 255, 255, 1.0)\n"
+ "rgba(255, 255, 255, 1.1)\n"
+ "rgba(255, 255, 255, 666)\n"
+ "rgba(255, 255, 255ab)\n"
+ "rgb(255, 255, 255, 344)\n\n"
+ "#p1 {background-color: rgba(255, 0, 0, 0.3);} /* red with opacity */\n"
+ "#p2 {background-color: rgba(0, 255, 0, 0.3);} /* green with opacity */\n"
+ "#p3 {background-color: rgba(0, 0, 255, 0.3);} /* blue with opacity */";
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