In Emacs regex, \n doesn't match a new line character \n.

  1. Am I correct that $ matches the position between a new line character and the character right before the new line character. e.g. for a string abc\n, $ matches the position between c and \n?
  2. What is the regex that matches \n?
  • 3
    Could you provide a minimum working example? Maybe I'm missing something, but (re-search-forward "\n") works fine for me.
    – Dan
    Feb 25, 2015 at 0:37
  • 1
    Your file might use \r\n for new lines and it may be that you need to include \r in your regexp, so abc\r\n instead of abc\n. Feb 25, 2015 at 15:53
  • @Dan: C-M-s and M-x occur both match \n to n. my buffer is in Fundamental mode. This happens to any text, so any text with new line or letter n is a working example
    – Tim
    Feb 27, 2015 at 2:17
  • 1
    @Jordon: C-M-s and M-x occur both match \r\n to rn
    – Tim
    Feb 27, 2015 at 2:18
  • 1
    @Tim, yes because if you are entering them interactively you'd need to do a quoted inserts, C-q C-m, and C-q C-j respectively. You could enter \r\n if you were entering them into a lisp string. Feb 27, 2015 at 3:27

2 Answers 2

  1. Yes. $ matches the end of the line, not the newline character which comes after the end of the line.
  2. Do C-M-s C-q C-j. C-q is the default binding for quoted-insert and works in the minibuffer too. This expression literally searches for a newline: C-j.
  • 1
    2. are you saying the new line character can not be represented as an escape sequence in Emacs regex?
    – Tim
    Feb 25, 2015 at 0:01
  • 3
    Whether searching for a newline interactively or via elisp (e.g. (looking-at "^J") where ^J is inserted by C-q C-j), the C-q C-j approach always works. But when using elisp functions like the same looking-at \n works too; M-: (looking-at "\n") RET will eval to true if the cursor is at the end of the line (and there's a newline after that). Feb 25, 2015 at 0:09
  • 3
    Just to mention, if using regexp-builder, you're able to recognize a new line with [\n].
    – Nsukami _
    Feb 25, 2015 at 0:29
  • 1
    @Nsukami_: C-M-s and M-x occur both match [\n] to n.
    – Tim
    Feb 27, 2015 at 2:19

As Dan comments, the regex that matches a newline is a newline.

You can represent a newline in a quoted string in elisp as "\n". There is no special additional regexp-specific syntax for this -- you just use a newline, exactly like any other literal character.

If you are entering a regexp interactively then you can insert the newline with C-qC-j, as kaushalmodi's answer points out.

  • 1
    Thanks! In emacs (not in elisp), is C-q C-j the only way to match a new line character? \n doesn't match a new line character.
    – Tim
    Apr 9, 2015 at 17:10
  • 2
    Yes. Well, more specifically, typing a newline is the only way to match a newline character when entering a regexp interactively (as there is no regexp escape sequence for a newline), and C-q C-j is the most reliable way to type a newline at a prompt.
    – phils
    Apr 9, 2015 at 22:21
  • There can be workarounds, see stackoverflow.com/a/20056634/3873799
    – alelom
    Mar 24, 2023 at 15:53
  • @alelom I don't understand what you're trying to say there. That Q&A appears to be about EOL conventions (which vary and may or may not involve carriage return characters), but this question is about matching a newline character.
    – phils
    Mar 24, 2023 at 23:05

Your Answer

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

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