16

The manual describes the regexp special characters ^ and $. Like in most regular expression dialects I know, they seem to match the start or end of a string. However, I've also discovered that there are ` and ' characters available. Based on the explanation found here, they seem to also match the start or end of strings. Could someone please explain the difference between these special characters, with an example and recommendation on when to use them?

When I look at the value of auto-mode-alist, they seem to be used interchangeably to match the end of strings:

(...
 ("\\.scss\\'" . scss-mode)
 ("\\.ya?ml$" . yaml-mode)
...)
  • 2
    When people use $ like that, they are banking on filenames not containing newlines. This is typically going to be a (very) safe assumption, but it's not guaranteed. Using \\' is therefore best practice. – phils May 5 '15 at 11:43
14

Your string might have an embedded newline character, in which case \' matches the end of the string but $ matches just before the newline char.

To quote the Elisp manual, node Regexp Special:

When matching a string instead of a buffer, `$' matches at the end of the string or before a newline character.

Similarly, \` matches the beginning of the string, but ^ matches just after the newline char. Again, the manual:

When matching a string instead of a buffer, `^' matches at the beginning of the string or after a newline character.

So for strings you generally want to use \` and \', especially if your code cannot know ahead of time whether there might be newline chars in the string. For text in a buffer you typically use ^ and $.

6
(string-match "^foo" "foo")         ; => 0
(string-match "\\`foo" "foo")       ; => 0
(string-match "^foo" "bar\nfoo")    ; => 4
(string-match "\\`foo" "bar\nfoo")  ; => nil

(string-match "foo$" "foo")         ; => 0
(string-match "foo\\'" "foo")       ; => 0
(string-match "foo$" "foo\nbar")    ; => 0
(string-match "foo\\'" "foo\nbar")  ; => nil

^ and $ match the start and end of lines.

\` and \' match the start and end of the entire string.

2
there are ` and ' characters available. [...] they seem to also match the start or end of strings

Special characters are: . * + ? ^ $ \ [.

Between brackets [], the following are special too: ] - ^

Many characters including backtick `, and single quote ', become special when they follow a backslash. See here for more details.

Could someone please explain the difference between these special characters, with an example and recommendation on when to use them?

There is a difference between beginning of string, and beginning of each line in the string.

Courtesy of Ergoemacs. You can read more here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.