Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs regex matching end of line followed by new line

Tags:

regex

emacs

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?

like image 695
Talespin_Kit Avatar asked Dec 13 '25 00:12

Talespin_Kit


1 Answers

$ 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 ‘\|’.
like image 164
Stefan Avatar answered Dec 15 '25 02:12

Stefan