             self.rawValue = rawValue
         }
 
-        public static let Basic =            RegexOptions(rawValue: 0)
-        public static let Extended =         RegexOptions(rawValue: 1)
-        public static let CaseInsensitive =  RegexOptions(rawValue: 2)
-        public static let ResultOnly =       RegexOptions(rawValue: 8)
-        public static let NewLineSensitive = RegexOptions(rawValue: 4)
+        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 {

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

            if result == 1 {
                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)
            }

            let offset = Int(regexMatches[0].rm_eo)
            string = string[string.startIndex.advancedBy(offset) ..< string.endIndex]
        }

        return groups
    }

    public func replace(string: String, withTemplate template: String, options: MatchOptions = []) -> String {
        var string = string
        let maxMatches = 10
        var totalReplacedString: String = ""


// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Gamut

struct RegexError : ErrorType {
    let description: String

    static func errorFromResult(result: Int32, var preg: regex_t) -> RegexError {
        var buffer = [Int8](count: Int(BUFSIZ), repeatedValue: 0)
        regerror(result, &preg, &buffer, buffer.count)
        let description = String.fromCString(buffer)!
