emacs-devel
[Top][All Lists]
Advanced

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

Re: cond*


From: Ihor Radchenko
Subject: Re: cond*
Date: Wed, 03 Jan 2024 15:48:35 +0000

Richard Stallman <rms@gnu.org> writes:

>        > In addition to the usual ‘rx’ syntax, RX-EXPR... can contain the
>        > following constructs:
>
>        > ‘(let REF RX-EXPR...)’
>             > Bind the symbol REF to a submatch that matches RX-EXPR....
>             > REF is bound in BODY-FORMS to the string of the submatch or
>             > ‘nil’, but can also be used in ‘backref’.
>
> I don't understand that description.  I don't see what this construct
> would look like inside of an (rx ...) pattern, let alone what it would
> mean.
>
> Can you show me a complete pattern example using (rx...) and this, and
> explain it?

Sure.

Consider the following `rx' form that matches lines like "#+begin:
something":

(rx
    line-start (0+ (any " \t")) "#+BEGIN:" (0+ (any " \t"))
    ;; matches "something"
    (1+ word))

This is equivalent to

"^[      ]*#\\+BEGIN:[   ]*[[:word:]]+"

Now, consider that we need to extract "something" from the match if the
match succeeds:

(pcase "  #+begin: something"
  ((rx
    line-start (0+ (any " \t")) "#+BEGIN:" (0+ (any " \t"))
    ;; matches "something"
    ;; (let VAR <sub-expression>) binds VAR to sub-expression match.
    (let matched-value (1+ word)))
   (format "We just matched \"%s\"" matched-value))) ; => "We just matched 
\"something\""

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



reply via email to

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