Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

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?

like image 846
user100464 Avatar asked Sep 15 '25 11:09

user100464


1 Answers

(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).

like image 173
Barmar Avatar answered Sep 17 '25 16:09

Barmar