emacs

Emacs Lisp: regular expression for "anything except close square bracket"?


In Emacs 24.2.1, I want to search backwards for the first character that is not a close square bracket, i.e. the ] character. For example, if I'm at the end this line:

123]4567

I would expect the point to move to the "7".

My first attempt was:

(re-search-backward "[^\]]" nil nil nil)

which moved to the point to the "3".

Although they seem to conflict with the documentation, I also tried these:

(re-search-backward "[^\\]]" nil nil nil)
(re-search-backward "[^\\\]]" nil nil nil)
(re-search-backward "[^\\\\]]" nil nil nil)

What is the correct regular expression?


Solution

  • (re-search-backward "[^]]")
    

    works for me in Emacs 22.2.50.1.

    You don't have to escape right bracket inside square brackets, you just have to make it the first character in the set (because an empty set is meaningless).