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.

4

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.

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 $.

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
  • 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 9 '15 at 17:44

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.