I have a couple of regular expressions that work fine using Emacs’ interactive search. They are “^\.” and “^#”. They search for a period and a hash at the beginning of the line.
I’m trying to write an interactive function that searches for these regular expressions on a line but I’m failing. My function is
(defun line-contains (regexp)
"Return true if the current line contains the passed regular expression."
(save-excursion
(beginning-of-line)
(search-forward-regexp regexp (point-at-eol) t)))
When I call
((or (line-contains "^\.")
(line-contains "^#"))
on the following line
if ($default)
it returns true even though that line doesn’t match the regular expressions when called by interactive search. I’m not sure what I’m doing wrong.
You have to escape the backslash in the string literal, like this