help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Word boundary with regular expression


From: Anselm Helbig
Subject: Re: Word boundary with regular expression
Date: Tue, 27 Sep 2005 15:13:54 +0200
User-agent: Wanderlust/2.11.30 (Wonderwall) Emacs/21.4 Mule/5.0 (SAKAKI)

At Tue, 27 Sep 2005 05:11:35 -0700 (PDT),
"fede (sent by Nabble.com)" <lists@nabble.com> wrote:
> I have the following problem: I'm programming in C language and I'd like
> to substitute every occurence of "my_type" with "another_type". So I'd like
> "my_type *mine;" to become "another_type *mine;" and so on, but NOT
> "my_type_2 *mine;" to become "another_type_2 *mine;"! I've tried the 
> following:
> 
> (query-replace-regexp "\\bmy_type\\b" "another_type" nil nil nil)
> 
> but the problem is that \b seems to include words terminating with '_'...
> any idea?

you're right, in emacs' c-mode, `_' is not thought to be part of a
`word', it's part of a `symbol'. this setting is made in the syntax
table, see (info "(Emacs)Syntax") for more information about it.

so you can either change the syntax table (just for the current
buffer) by issuing a command like the following:

        M-: (modify-syntax-entry ?_  "w")

from the buffer you're editing your c-file in. this means, put the
character "_" in the "w"ord-class. your original regex should work
then.

an alternative would be to check for whitespace in your regex, like
this (untested):

        (query-replace-regexp "\\bmy_type\\(\\s \\)" "another_type\\1" nil nil 
nil)

not nice, but should work as well. 

i didn't yet fiddle much with syntax-tables myself, so i don't know if
there would be any ill side-effects if you changed the syntax-table
for c-mode permanently. 

HTH, 

anselm


reply via email to

[Prev in Thread] Current Thread [Next in Thread]