I have a regex that I'm trying to use in Swift to parse out the color values of a string in the form of "rgba(255, 255, 255, 1)"
My regex string is this:
^rgba?\(\s*(\d{1,3}(?:\.\d+)?\%?)\s*,\s*(\d{1,3}(?:\.\d+)?\%?)\s*,\s*(\d{1,3}(?:\.\d+)?\%?)\s*(?:\s*,\s*(\d+(?:\.\d+)?)\s*)?\)$
You can see it working here:
When I escape the backslashes and attempt to use it with NSRegularExpression
it no longer matches the given string.
^rgba?\\(\\s*(\\d{1,3}(?:\\.\\d+)?\\%?)\\s*,\\s*(\\d{1,3}(?:\\.\\d+)?\\%?)\\s*,\\s*(\\d{1,3}(?:\\.\\d+)?\\%?)\\s*(?:\\s*,\\s*(\\d+(?:\\.\\d+)?)\\s*)?\\)$
Is there something I am doing wrong?
Code Example:
import Foundation
var pattern = "^rgba?\\(\\s*(\\d{1,3}(?:\\.\\d+)?\\%?)\\s*,\\s*(\\d{1,3}(?:\\.\\d+)?\\%?)\\s*,\\s*(\\d{1,3}(?:\\.\\d+)?\\%?)\\s*(?:\\s*,\\s*(\\d+(?:\\.\\d+)?)\\s*)?\\)$"
var string = "rgba(255, 234, 243, 1)"
var regex = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions(0), error: nil)
regex.numberOfMatchesInString(string, options: nil, range: NSMakeRange(0, countElements(string))) // Yields 1. Should yield 4