Emacs Regex Quirk: Matching beginning/end of line/string

Perm URL with updates: http://xahlee.org/emacs/emacs_regex_begin_end_line_string.html

, 2011-09-29, …, 2011-11-28

This page is a tutorial on emacs regex. 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?


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 "^ +" "•"
    (1) this
    (2) that

Here's the result:

•(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.


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

See also: Text Pattern Matching in Emacs (emacs regex tutorial).

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs