   /// Expand template as a URI Template using the given variables
   public func expand(_ variables: [String: Any]) -> String {
     return regex.substitute(template) { string in
+#if swift(>=4.0)
+      var expression = String(string[string.characters.index(after: string.startIndex)..<string.characters.index(before: string.endIndex)])
+      let firstCharacter = String(expression[..<expression.characters.index(after: expression.startIndex)])
+#else
       var expression = string.substring(with: string.characters.index(after: string.startIndex)..<string.characters.index(before: string.endIndex))
       let firstCharacter = expression.substring(to: expression.characters.index(after: expression.startIndex))
+#endif
 
       var op = self.operators.filter {
         if let op = $0.op {

    if let prefix = prefix {
      if value.characters.count > prefix {
        let index = value.characters.index(value.startIndex, offsetBy: prefix, limitedBy: value.endIndex)
        return expand(value: value.substring(to: index!))
      }
    }


      if expression.hasPrefix("{") && expression.hasSuffix("}") {
        let startIndex = expression.characters.index(after: expression.startIndex)
        let endIndex = expression.characters.index(before: expression.endIndex)
#if swift(>=4.0)
        return self.regexForExpression(String(expression[startIndex..<endIndex]))
#else
        return self.regexForExpression(expression.substring(with: startIndex..<endIndex))
#endif
      } else {
        return NSRegularExpression.escapedPattern(for: expression)
      }
