Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSRegularExpression cannot find capturing group matches

I'm trying to parse a string using one regular expression pattern.

Here is the pattern:

(\")(.+)(\")\s*(\{)

Here is the text to be parsed:

"base" {

I want to find these 4 capturing groups:

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

I am using the following code trying to capture those 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 the entire string instead of 4 groups.

Is that even possible to get those groups using NSRegularExpression?

like image 460
Tomasz Szulc Avatar asked Jul 19 '15 08:07

Tomasz Szulc


1 Answers

Yes, of course it is possible. You just have to change your current logic for finding the actual groups:

func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
    var results = [String]()

    let textRange = NSMakeRange(0, text.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))

    do {
        let regex = try NSRegularExpression(pattern: pattern, options: [])
        let matches = regex.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange)

        for index in 1..<matches[0].numberOfRanges {
            results.append((text as NSString).substringWithRange(matches[0].rangeAtIndex(index)))
        }
        return results
    } catch {
        return []
    }
}

let pattern = "(\")(.+)(\")\\s*(\\{)"
print(matchesInCapturingGroups("\"base\" {", pattern: pattern))

You actually only get 1 match. You have to go into that match and in there you will find the captured groups. Note that I omit the first group since the first group represents the entire match.

This will output

[""", "base", """, "{"]

Note the escaped regex string and make sure that you are using the same one.

like image 144
luk2302 Avatar answered Nov 15 '22 08:11

luk2302