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?
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.
| Special Regex Char | Matches |
|---|---|
^ | beginning of {line, string, buffer} |
$ | end of {line, string, buffer} |
\` | beginning of {string, buffer} |
\' | end of {string, buffer} |
See also: common emacs regex patterns.