title: Emacs lisp regex to extract current relative path from buffer-file-name
body: <p>I am trying to extract the current directory relative to the home directory from the <code>buffer-file-name</code> variable. Consider:</p>
  
  <pre><code>(defun test ()
    (interactive)
    (let ((fn "/home/user/dir1/dir2/file.txt"))
      (when (string-match "^.*?/user/" fn)
        (let ((temp (replace-match "${HOME}/" nil nil fn)))
          (when (string-match "/.*?$" temp)
            (message (replace-match "/" nil nil temp)))))))
  </code></pre>
  
  <p>Here I would like to get <code>${HOME}/dir1/dir2/</code> but instead I get <code>${HOME}/</code>. What am I missing here?</p>