Reputation: 21877
The match for the empty line followed by the new line fails for the regex ^$^J
but succeeds for the regex ^^J
. What is wrong with the former regex?
Upvotes: 3
Views: 2010
Reputation: 28531
$
in a regexp usually matches the empty string at an end of line, but in reality this is only true if the $
appears at specific places in the regexp (e.g. at the end of the regexp, or at the end of a sub-group, IIRC). If the $
appears "in the middle" it just matches the $
character. Same applies to ^
. E.g. (string-match "a^b$c" "1a^b$c2")
returns 1.
C-hig (emacs) Regexps
documents this behaviour:
‘^’
is a special character that matches the empty string, but only at
the beginning of a line in the text being matched. Otherwise it
fails to match anything. Thus, ‘^foo’ matches a ‘foo’ that occurs
at the beginning of a line.
For historical compatibility reasons, ‘^’ can be used with this
meaning only at the beginning of the regular expression, or after
‘\(’ or ‘\|’.
‘$’
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 ‘\|’.
Upvotes: 5