Improve HEREDOC regexp

  * Exclude trailing blank line from the match group
  * Fix backslash handling