I'm trying to search for the last line in a string (lines separated by \n) using string match but I cannot make it work as intended. The code below evaluates to 0 while I would have expected 4. I guess my regex is wrong (it was supposed to be a newline (\n) followed by anything but a newline (.*) until the end of the string ($).

(string-match "\n.*$" "\n \n \n ")

1 Answer 1


$ matches at the end of a line, not the end of a string. If you want to match at the end of a string you need to use the \' operator:

(string-match "\n.*\\'" "\n \n \n ")
=> 4

See (info "(elisp)Regexp Backslash") for more details (or Regexp-Backslash)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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