unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: how to use a variable inside Emacs regex features ?
Date: Thu, 23 May 2019 08:53:08 +0200	[thread overview]
Message-ID: <20190523065308.GA28035@tuxteam.de> (raw)
In-Reply-To: <CAH0GyZAwGL7jLDby7MSeVVqQWxu0m1vJZkfsHGaoxzWeZ+L5Qw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 868 bytes --]

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 #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  reply	other threads:[~2019-05-23  6:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-23  3:15 how to use a variable inside Emacs regex features ? Budi
2019-05-23  3:52 ` Drew Adams
2019-05-23  4:46   ` Budi
2019-05-23  6:53     ` tomas [this message]
2019-05-23 16:37       ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190523065308.GA28035@tuxteam.de \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).