     /// 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.regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: template)
+        return regex.nsRegex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: template)
     }
 }
 


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

//  THE SOFTWARE.
//

@frozen
public struct Regex {
    
    public let nsRegex: NSRegularExpression
    
    @inlinable
    public init(_ regex: NSRegularExpression) {
        self.nsRegex = regex
    }
    
    @inlinable
    public init(pattern: String, options: NSRegularExpression.Options = []) throws {
        self.nsRegex = try NSRegularExpression(pattern: pattern, options: options)
    }
    
    /// Returns the regular expression pattern.
    @inlinable
    public var pattern: String {
        return nsRegex.pattern
    }
    
    /// Returns the options used when the regular expression option was created.
    @inlinable
    public var options: NSRegularExpression.Options {
        return nsRegex.options
    }
    
    /// Returns the number of capture groups in the regular expression.
    ///
    /// A capture group consists of each possible match within a regular expression. Each capture group can then be used in a replacement template to insert that value into a replacement string.
    @inlinable
    public var numberOfCaptureGroups: Int {
        return nsRegex.numberOfCaptureGroups
    }
}

extension Regex: ExpressibleByStringLiteral {
    
    @inlinable
    public init(stringLiteral value: StringLiteralType) {
        try! self.init(pattern: value)
    }
