 
 extension Regex: CustomStringConvertible {
     
+    @inlinable
     public var description: String {
         return pattern
     }

extension String: RegularExpressionMatchable {
    
    /// Returns the number of matches of the regular expression in the string.
    public func count(regex: Regex) -> Int {
        let nsstring = NSString(string: self)
        let range = NSRange(location: 0, length: nsstring.length)
        return regex.regex.numberOfMatches(in: self, options: [], range: range)
    }
    
    /// Returns true if any match of the regular expression in the string.
    public func isMatch(regex: Regex) -> Bool {
        let nsstring = NSString(string: self)
        let range = NSRange(location: 0, length: nsstring.length)
        return regex.regex.firstMatch(in: self, options: [], range: range) != nil
    }
    
    /// Returns the first match of the regular expression in the string.
    public func firstMatch(regex: Regex) -> String? {
        let nsstring = NSString(string: self)
        let range = NSRange(location: 0, length: nsstring.length)
        let match_result = regex.regex.firstMatch(in: self, options: [], range: range)
        return match_result.map { nsstring.substring(with: $0.range) }
    }
    
    /// Returns an array containing all the matches of the regular expression in the string.
    public func match(regex: Regex) -> [String] {
        let nsstring = NSString(string: self)
        let range = NSRange(location: 0, length: nsstring.length)
        var match_result = [String]()
        regex.regex.enumerateMatches(in: self, options: [], range: range) { result, _, _ in
            if let _result = result {
                match_result.append(nsstring.substring(with: _result.range))
            }

    /// The replacement is treated as a template, with $0 being replaced by the contents of the matched range, $1 by the contents of the first capture group, and so on.
    /// Additional digits beyond the maximum required to represent the number of capture groups will be treated as ordinary characters, as will a $ not followed by digits.
    /// Backslash will escape both $ and itself.
    @inlinable
    public func replace(regex: Regex, template: String) -> String {
        let nsstring = NSString(string: self)
        let range = NSRange(location: 0, length: nsstring.length)
        return regex.nsRegex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: template)
    }
}

