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

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

Re: how to use a variable inside Emacs regex features ?


From: tomas
Subject: Re: how to use a variable inside Emacs regex features ?
Date: Thu, 23 May 2019 08:53:08 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Thu, May 23, 2019 at 11:46:29AM +0700, Budi wrote:
> (setq ntimes 7)
> re-search-forward "hello\\{ntimes\\}"
> 
> How is above supposed to work ?

It's not supposed to work ;-)

(the specific construct above takes a constant between \{ and \}).

Usually you construct your string using whatever other facilities
Elisp has, e.g.

(let* ((ntimes 17)
       (re (format "fo\\{%d\\}" ntimes)))
  (re-search-forward re)) ; searches for 'f' followed by 17 'o's

Be extra careful for variables containing regular expression
special characters (in the above case of an int, there is no
danger). Use the function 'regexp-quote' as needed.

If you are doing more complex things, you might like 'rx', which
allows you to write regular expressions in S-expression syntax,
thus giving you access to template macros and all that goodness.

Cheers
-- t

Attachment: signature.asc
Description: Digital signature


reply via email to

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