         regExPattern += "("
         curParen += 1
 
-        let substr = patternStr.substring(
-          with: Range<String.Index>(
-            uncheckedBounds: (
-              patternStr.index(patternStr.startIndex, offsetBy: 2),
-              End.lowerBound
-            )
-          )
-        )
+        let substr = patternStr.substring(to: end)
         let (res, paren) = self.addRegExToRegEx(substr, curParen)
         curParen = paren
         if res {

      // RegEx matches.
      if patternStr.range(of: "{{")?.lowerBound == patternStr.startIndex {
        // This is the start of a regex match.  Scan for the }}.
        guard let End = patternStr.range(of: "}}") else {
          let loc = CheckLoc.inBuffer(pattern.baseAddress!, buf)
          diagnose(.error, loc, "found start of regex string with no end '}}'")
          return true

    var bracketDepth = 0

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