4

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 ")
8

$ 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, privacy policy and cookie policy

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