     parse_string_literal(t, acc <> h, replacement, empty_line_replacement)
   end
 
-  defp pad_replaced_heredoc(acc, delimiter) do
-    no_of_chars_to_replace =
-      if acc =~ ~r/\n\Z/m do
-        0
-      else
-        [leading_string] = Regex.run(~r/[^\n]*\Z/m, acc)
-
-        String.length(leading_string)
-      end
-
+  defp pad_replaced_heredoc(acc, _delimiter, current_line, byte_index_start) do
+    no_of_chars_to_replace = String.length(current_line) - 1
     pad_string = "\n" <> String.pad_leading("", no_of_chars_to_replace)
-
-    {byte_index, _match_size} =
-      Regex.scan(~r/#{delimiter}/m, acc, return: :index)
-      |> List.last()
-      |> List.first()
-
-    length_after_byte_index = byte_size(acc) - byte_index
 
     new_acc =
       acc
-      |> :binary.part(byte_index, length_after_byte_index)
+      |> :binary.part(byte_index_start, -1)
       |> String.replace(~r/\n(.{#{no_of_chars_to_replace}})/, pad_string)
 
-    :binary.part(acc, 0, byte_index) <> new_acc
+    :binary.part(acc, 0, byte_index_start) <> new_acc
   end
 end

        acc <> unquote(sigil_start),
        unquote(sigil_end),
        replacement,
        empty_line_replacement
      )
    end
  end

           acc,
           unquote(sigil_end),
           replacement,
           empty_line_replacement,
           current_line,
           byte_index_start
         ) do
      parse_removable_heredoc_sigil(
        t,
        acc,
        unquote(sigil_end),
        replacement,
        empty_line_replacement,
        current_line,
        byte_index_start
      )
    end

