Emacs Regex Quirk: Matching Beginning/End of Line/String/Buffer

By Xah Lee. Date: . Last updated: .

This page is a tutorial on Emacs: Regular Expression . Suppose you want to write a function that removes spaces in front of a string. You'd use a regex like this:

(replace-regexp-in-string "^ +" "" myString)

Here, the ^ means beginning of string, right?

WRONG!

In emacs regex, ^ matches beginning of string, but also beginning of each line in the string. Try to evaluate the following (place cursor at end then call eval-last-sexp.):

(replace-regexp-in-string "^ +" "•"
"
  like
    (1) this
    (2) that
")

Here's the result:

"
•like
•(1) this
•(2) that
"

To match just the beginning of a string, use \`. Like this:

;; Remove space/tab/newline in beginning myStr
(replace-regexp-in-string "\\`[ \t\n]*" "" myStr)

Similarly, the $ matches the endings of {buffer, string, line}. To just match ending of {buffer, string}, use \'. In lisp code, you'll need to double the backslash.

Summary

Special Regex CharMatches
^beginning of {line, string, buffer}
$end of {line, string, buffer}
\`beginning of {string, buffer}
\'end of {string, buffer}