Home > Mobile >  How to get value of R, G, and B from RGB string in dart?
How to get value of R, G, and B from RGB string in dart?

Time:10-14

I've tried the following code in dart, but unable to get solution.

RegExp rgb = RegExp(r"^rgb\(([0-9] ),\s*([0-9] ),\s*([0-9] )\)$");
final match = rgb.allMatches("rgb(127, 127, 126)");

I can easily do it in javascript with the above regular expression using rgb.exec(String) method which returns array.

CodePudding user response:

You can get the capture groups using the groups method:

RegExp rgb = RegExp(r"^rgb\(([0-9] ),\s*([0-9] ),\s*([0-9] )\)$");
final matches = rgb.allMatches("rgb(127, 127, 126)");
for (final match in matches) {
    final colors = match.groups([1, 2, 3]);
    print('red: ${colors[0]}, green: ${colors[1]}, blue: ${colors[2]}');
}
  • Related