             var pos: String.Index = str.startIndex
 
             for match in matches {
-                let range = match.ranges[0]
-
-                buffer += str[pos..<range.startIndex]
-                buffer += "\\"
-                buffer += str[range]
-
-                pos = range.endIndex
+                if let range = match.ranges[0] {
+                    buffer += str[pos..<range.startIndex]
+                    buffer += "\\"
+                    buffer += str[range]
+
+                    pos = range.endIndex
+                }
             }
 
             buffer += str[pos..<str.endIndex]

        /** The string that was matched. */
        public let subject: String
        /** Ranges of the matches, with the entire pattern match in 0. */
        public let ranges: [Range<String.Index>]
        /** Count of the capture groups, including the entire pattern match. */
        public var count: Int { return ranges.count }

        init(subject: String, matchResult: NSTextCheckingResult) {
            self.subject = subject

            var ranges: [Range<String.Index>] = []

            // Check the overall match first. Shouldn't happen
            if matchResult.range.location == NSNotFound {

        return search(subject) != nil
    }
}
