Emacs search words with underscores not considered as word boundaries

379 Views Asked by At

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.

2

There are 2 best solutions below

1
Aparna venugopal On

I don't use EMAC, but in Notepad++ or in python \bfoo\b should give you the required result. But since it is not working out for you, this should work: \b(?<!_)foo(?!_)\b

(?<!_) - negative look behind to ensure that _ does not precede
(?!_) - negative look ahead to ensure that _ does not follow

See demo

0
Youjun Hu 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.