         }
         regExPattern += ")"
 
-        patternStr = patternStr.substring(from: patternStr.index(End.lowerBound, offsetBy: 2))
+        patternStr = patternStr.substring(from: patternStr.index(end, offsetBy: 2))
         continue
       }
 

        // Find the closing bracket pair ending the match.  End is going to be an
        // offset relative to the beginning of the match string.
        let regVar = patternStr.substring(from: patternStr.index(patternStr.startIndex, offsetBy: 2))
        guard let end = self.findRegexVarEnd(regVar) else {
          let loc = CheckLoc.inBuffer(pattern.baseAddress!, buf)
          diagnose(.error, loc, "invalid named regex reference, no ]] found")
          return true

    var bracketDepth = 0

    while let firstChar = string.characters.first {
      if string.hasPrefix(terminator) && bracketDepth == 0 {
        return offset
      }
      if firstChar == "\\" {
