Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs search by exact regex match instead of partial

I'm looking through a very large hydrodynamics code in c which has, often, some very poor variables choices. Including a global variable named just 'g'. Similarly, there's a file with a variable named 'geom' and lots of other variables which contain the substring 'geom' (e.g. geometry, geomAL, geom_arb, etc.).

Is there any way to search for variables that exactly match a regex, instead of partially?

For example: searching for 'geom' does not match 'geomAL'. Obviously emacs doesn't a priori know where a variable starts or ends, but could this be constructed as a function for c-mode?

like image 904
DilithiumMatrix Avatar asked Aug 30 '13 13:08

DilithiumMatrix


3 Answers

The Emacs regular expression engine (C-M-s <regexp>) has various operands for this sort of thing, such as the word boundary \< and \> zero-width assertions. So \<geom\> would match geom alone and (depending on your mode's syntax table) perhaps also the prefix in geom_something. Try \<geom\>[^_] if you need to exclude the underscore suffix.

like image 172
tripleee Avatar answered Sep 26 '22 02:09

tripleee


You can use C-u C-s \_<g\_> which will search for the symbol g using a regular expression search with symbol-boundary markers. Or in a recent enough Emacs you can do M-s _ g which will do essentially the same (M-s is the "search prefix key" in which M-s _ is isearch-forward-symbol).

like image 31
Stefan Avatar answered Sep 26 '22 02:09

Stefan


Have you tried out the Emacs TAGS system? It should be able to parse the vars out and it might offer exact lookups. See here: http://www.emacswiki.org/emacs/EmacsTags

Generate the tags table with the etags helper:

etags *.c  

Look for a tag with

M-. your-var-name
like image 26
mvw Avatar answered Sep 23 '22 02:09

mvw