NSRegularExpression cannot find capturing group matches

I’m trying to parse few things using one regular expression pattern.

Here is the pattern:


Here is the text to be parsed:

"base" {

I want to find these 4 capturing groups:

1. "
2. base
3. "
4. {

And the code that is trying to find that 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 {
            // this match = <NSExtendedRegularExpressionCheckingResult: 0x7fac3b601fd0>{0, 8}{<NSRegularExpression: 0x7fac3b70b5b0> (")(.+)(")s*({) 0x1}
            results.append(self.substring(text, range: match.range))

    return results

Unfortunately it is able to find only one group with range (0, 8) which is equal to: "base" {. So it finds one group which is entire string instead of 4 groups.

Is that even possible to get those groups using NSRegularExpression?

Thank you in advance.

Source: regex

Leave a Reply