             var regexMatches = [regmatch_t](count: maxMatches, repeatedValue: regmatch_t())
             let result = regexec(&preg, string, regexMatches.count, &regexMatches, options.rawValue)
 
-            if result == 1 {
+            if result == REG_NOMATCH {
                 break
             }
 
             for var j = 1; regexMatches[j].rm_so != -1; j++ {
                 let start = Int(regexMatches[j].rm_so)
                 let end = Int(regexMatches[j].rm_eo)
-                let match = string[string.startIndex.advancedBy(start) ..<  string.startIndex.advancedBy(end)]
-                groups.append(match)
+                if let match = String(string.utf8[string.utf8.startIndex.advancedBy(start) ..< string.utf8.startIndex.advancedBy(end)]) {
+                    groups.append(match)
+                }
             }
 
             let offset = Int(regexMatches[0].rm_eo)
-            string = string[string.startIndex.advancedBy(offset) ..< string.endIndex]
+            if let offsetString = String(string.utf8[string.utf8.startIndex.advancedBy(offset) ..< string.utf8.endIndex]) {
+                string = offsetString
+            } else {
+                break
+            }
         }
 
         return groups
     }
 
-    public func replace(string: String, withTemplate template: String, options: MatchOptions = []) -> String {
-        var string = string
+    public func replace(var string: String, withTemplate template: String, options: MatchOptions = []) -> String {
         let maxMatches = 10
         var totalReplacedString: String = ""
 

            var regexMatches = [regmatch_t](count: maxMatches, repeatedValue: regmatch_t())
            let result = regexec(&preg, string, regexMatches.count, &regexMatches, options.rawValue)

            if result == 1 {
                break
            }

            let start = Int(regexMatches[0].rm_so)
            let end = Int(regexMatches[0].rm_eo)

            var replacedString = string
            replacedString.replaceRange(string.startIndex.advancedBy(start) ..<  string.startIndex.advancedBy(end), with: template)

            let templateDelta = template.utf8.count - (end - start)
            let templateDeltaIndex = replacedString.startIndex.advancedBy(Int(end + templateDelta))

            replacedString = replacedString[replacedString.startIndex ..< templateDeltaIndex]

            totalReplacedString += replacedString
            string = string[string.startIndex.advancedBy(end) ..< string.endIndex]
        }
        
        return totalReplacedString + string
    }
}
            self.rawValue = rawValue
        }

        public static let Basic =            RegexOptions(rawValue: REG_BASIC)
        public static let Extended =         RegexOptions(rawValue: REG_EXTENDED)
        public static let CaseInsensitive =  RegexOptions(rawValue: REG_ICASE)
        public static let ResultOnly =       RegexOptions(rawValue: REG_NOSUB)
        public static let NewLineSensitive = RegexOptions(rawValue: REG_NEWLINE)
    }

    public struct MatchOptions: OptionSetType {
