[comp.emacs] GNU Emacs string modification question

karl@tut.cis.ohio-state.edu (Karl Kleinpaste) (04/27/88)

I have a variable which has been setq'd to a string value.  The string
may contain characters which are special to RE searches, but I need
the characters to be used literally in searches.  (Specifically, I am
looking at newsgroup name searches in the vn emulator, possibly
containing +'s, e.g., comp.lang.c++.)  I cannot avoid performing the
search as re-search-forward; plain search-forward will not do, due to
the need to find "[:!]" following the newsgroup name.  Can anyone give
me a simple function which takes a string argument and inserts
backslashes (\) in front of RE-special characters within the string?

--Karl

jr@PEBBLES.BBN.COM (John Robinson) (04/27/88)

I think the function you want might make use of:

 regexp-quote:
 Return a regexp string which matches exactly STRING and nothing else.

provided you can call it prior to puttin g the real regexp stuff into
the search string.

/jr
jr@bbn.com or jr@bbn.uucp

Ram-Ashwin@cs.yale.edu (Ashwin Ram) (04/27/88)

In article <11715@tut.cis.ohio-state.edu>, karl@tut (Karl Kleinpaste) writes:
>                                                        Can anyone give
> me a simple function which takes a string argument and inserts
> backslashes (\) in front of RE-special characters within the string?

Try:

    regexp-quote:
    Return a regexp string which matches exactly STRING and nothing else.

For example:

    (regexp-quote "comp.lang.c++")
    "comp\\.lang\\.c\\+\\+"


-- Ashwin.

ARPA:    Ram-Ashwin@cs.yale.edu
UUCP:    {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin
BITNET:  Ram@yalecs

matt@oddjob.UChicago.EDU (Schizophrenic Solipsist) (04/28/88)

Karl Kleinpaste writes:
) I have a variable which has been setq'd to a string value.  The string
) may contain characters which are special to RE searches, but I need
) the characters to be used literally in searches.

regexp-quote:
Return a regexp string which matches exactly STRING and nothing else.

) plain search-forward will not do, due to
) the need to find "[:!]" following the newsgroup name.

(re-search-forward (concat (regexp-quote newsgroup) "[:!]"))

				Matt

michael@pbinfo.UUCP (Michael Schmidt) (04/28/88)

Neulich schrieb karl@tut.cis.ohio-state.edu (Karl Kleinpaste):
  Can anyone give
  me a simple function which takes a string argument and inserts
  backslashes (\) in front of RE-special characters within the string?

Is something wrong with "regexp-quote"?

regexp-quote:
Return a regexp string which matches exactly STRING and nothing else.

       Michael Schmidt
-- 
    Michael Schmidt, Universitaet-GH Paderborn, FB 17, Warburger Str.100,
                     D-4790 Paderborn, West Germany
Mail:   michael@pbinfo.UUCP         or          michael%pbinfo@uunet.uu.net

msb@ho5cad.ATT.COM (04/28/88)

In article <11715@tut.cis.ohio-state.edu> karl@tut.cis.ohio-state.edu (Karl Kleinpaste) writes:

> I have a variable which has been setq'd to a string value.  The string
> may contain characters which are special to RE searches, but I need
> the characters to be used literally in searches.  (Specifically, I am
> looking at newsgroup name searches in the vn emulator, possibly
> containing +'s, e.g., comp.lang.c++.)  I cannot avoid performing the
> search as re-search-forward; plain search-forward will not do, due to
> the need to find "[:!]" following the newsgroup name.  Can anyone give
> me a simple function which takes a string argument and inserts
> backslashes (\) in front of RE-special characters within the string?
> 
> --Karl

spell.el uses a function regexp-quote.  Should do exactly what you
want.  Here is the function-help.

regexp-quote:
Return a regexp string which matches exactly STRING and nothing else.

----------------------------------------------------------------------
<cute quote>            Michael S. Balenger             (201) 949-8789
<cute disclaimer>       AT&T Bell Labs
                        Crawfords Corner Road
ihnp4!ho5cad!msb        Holmdel, NJ   07733

sbb@smpvax1.UUCP (05/03/88)

   From: Karl Kleinpaste <trwrb!ucbvax!tut.cis.ohio-state.edu!karl>

   I have a variable which has been setq'd to a string value.  The string
   may contain characters which are special to RE searches, but I need
   the characters to be used literally in searches.  (Specifically, I am
   looking at newsgroup name searches in the vn emulator, possibly
   containing +'s, e.g., comp.lang.c++.)  I cannot avoid performing the
   search as re-search-forward; plain search-forward will not do, due to
   the need to find "[:!]" following the newsgroup name.  Can anyone give
   me a simple function which takes a string argument and inserts
   backslashes (\) in front of RE-special characters within the string?

Karl,
GNU Emacs comes with a function called "regexp-quote".  It takes a
string that may contain regular expression chars and puts in suitable
quoting characters so that the returned string will match (in regexp
contexts) exactly the string passed as its argument.

steve
-----------------------
Steve Byrne				...ucbvax!trwrb!smpvax1!sbb
Inference Corp.				(213) 417-7997
5300 W. Century Blvd.
Los Angeles, CA  90045