I am trying to parse a string using a single regex pattern.
Here is the template:
(\")(.+)(\")\s*(\{)
Here is the text to parse:
"base" {
I want to find these 4 capture groups:
1. "
2. base
3. "
4. {
I am using the following code trying to capture these groups
class func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
var results = [String]()
let textRange = NSMakeRange(0, count(text))
var index = 0
if let matches = regexp(pattern)?.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange) as? [NSTextCheckingResult] {
for match in matches {
results.append(self.substring(text, range: match.range))
}
}
return results
}
Unfortunately, he could find only one group with a range (0, 8)that is equal to: "base" {. Thus, he finds one group, which is a whole line instead of 4 groups.
Is it even possible to get these groups using NSRegularExpression?
source
share