unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how to use a variable inside Emacs regex features ?
@ 2019-05-23  3:15 Budi
  2019-05-23  3:52 ` Drew Adams
  0 siblings, 1 reply; 5+ messages in thread
From: Budi @ 2019-05-23  3:15 UTC (permalink / raw)
  To: help-gnu-emacs

how to use a variable inside Emacs' regex features such as
re-search-forward, looking-at, and all the rest ?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: how to use a variable inside Emacs regex features ?
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Drew Adams @ 2019-05-23  3:52 UTC (permalink / raw)
  To: Budi, help-gnu-emacs

> how to use a variable inside Emacs' regex features such as
> re-search-forward, looking-at, and all the rest ?

Use it inside how? where?

Maybe tell us what you really want to do.

Give an example, perhaps, to show what you're asking about.  In any case, `C-h f re-search-forward' should tell you what you need to know about its arguments.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable inside Emacs regex features ?
  2019-05-23  3:52 ` Drew Adams
@ 2019-05-23  4:46   ` Budi
  2019-05-23  6:53     ` tomas
  0 siblings, 1 reply; 5+ messages in thread
From: Budi @ 2019-05-23  4:46 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

(setq ntimes 7)
re-search-forward "hello\\{ntimes\\}"

How is above supposed to work ?

On 5/23/19, Drew Adams <drew.adams@oracle.com> wrote:
>> how to use a variable inside Emacs' regex features such as
>> re-search-forward, looking-at, and all the rest ?
>
> Use it inside how? where?
>
> Maybe tell us what you really want to do.
>
> Give an example, perhaps, to show what you're asking about.  In any case,
> `C-h f re-search-forward' should tell you what you need to know about its
> arguments.
>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable inside Emacs regex features ?
  2019-05-23  4:46   ` Budi
@ 2019-05-23  6:53     ` tomas
  2019-05-23 16:37       ` Michael Heerdegen
  0 siblings, 1 reply; 5+ messages in thread
From: tomas @ 2019-05-23  6:53 UTC (permalink / raw)
  To: help-gnu-emacs

[-- 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 --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use a variable inside Emacs regex features ?
  2019-05-23  6:53     ` tomas
@ 2019-05-23 16:37       ` Michael Heerdegen
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Heerdegen @ 2019-05-23 16:37 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

<tomas@tuxteam.de> writes:

> 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.

This is a task for rx indeed - or rx-to-string when the regexp depends
on the run-time value of a variable:

(let ((ntimes 8))
  (re-search-forward
   (rx-to-string `(= ,ntimes "hello"))))


Michael.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-05-23 16:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2019-05-23 16:37       ` Michael Heerdegen

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).