2

I want to match two or more consecutive lines with numbers only, e.g.:

abc
1
2
abc

In Emacs, my regex is \(^ *[[:digit:]]+ *$^J\)\{2,\}, but it doesn't match any. I find \(^ *[[:digit:]]+ *^J\)\{2,\} can match.

Isn't it true that $ doesn't consume a character, but signals the end of a line (just before a newline character)?

In Python, I remember it doesn't matter that I specify $\n or \n. So why does it matter in Emacs? Thanks.

Tim
  • 5,007
  • 7
  • 32
  • 61

3 Answers3

5

Isn't it true that $ doesn't consume a character, but signals the end of a line (just before a newline character)?

$ can only be used at the end of a regexp, or just before \) or \|.

From the manual:

$

is similar to ‘^’ but matches only at the end of a line. Thus, ‘x+$’ matches a string of one ‘x’ or more at the end of a line. For historical compatibility reasons, ‘$’ can be used with this meaning only at the end of the regular expression, or before ‘)’ or ‘\|’.

What these "historical reasons" are, I don't know.

So if you want to use $ and ^J you can replace $^J with \($\)^J and it will work as expected, though there is no need to express the end of a line in this redundant way.

Jordon Biondo
  • 12,455
  • 2
  • 43
  • 62
3

Note that the $ is redundant when used just before a ^J character (aka LF or newline or \n). So while @JordonBiondo has the right answer, I think a better alternative is to just ditch the $.

Stefan
  • 26,404
  • 3
  • 48
  • 85
2

Using \n to represent the newline should work (compare this description of Emacs newline representation. This re puts point just after the 2:

(re-search-forward "\\(^\\s-*[0-9]+\\s-*\n\\)\\{2,\\}" nil t)

abc
1
2
abc
Dan
  • 32,980
  • 7
  • 102
  • 169
  • This only works when the regexp is defined in an elisp string, when you are enterring a regexp interactively, you must insert a literal newline. Since OP's regexps are not double quoted I assume this is what they are doing. – Jordon Biondo Apr 09 '15 at 17:44