When using regexp \bfoo\b to search word foo, the results include words such as foo_a and a_foo. How to exclude these words from the results? i.e., how to ask emacs not to consider underscores as word boundaries.
Emacs search words with underscores not considered as word boundaries
379 Views Asked by Youjun Hu AtThere are 2 best solutions below
On
I finally found a solution:
By default, the underscore _ is not a word-constituent character (in other words, it is a word-boundary). We can define the underscore as a word-constituent character via adding
(modify-syntax-entry ?_ "w")
in .emacs file.
To make this work in a major mode, we need to add this definition to the mode hook:
(defun yjf90 () (modify-syntax-entry ?_ "w"))
(add-hook 'f90-mode-hook 'yjf90)
See this
Update:
More accurately, we should distinguish between word boundary and symbol boundary. The former is \b, the latter: the start and end of a symbol are \_< and \_>, respectively (this is so awkward that I am not aware of its existence after using Emacs for many years). Most programming languages consider the underscore as a part of a symbol, so the straightforward solution to the question is to use \_<foo\_> as the searching regexp.
I don't use EMAC, but in Notepad++ or in python
\bfoo\bshould give you the required result. But since it is not working out for you, this should work:\b(?<!_)foo(?!_)\bSee demo