emacs-devel
[Top][All Lists]
Advanced

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

Re: Pattern matching on match-string groups #elisp #question


From: Basil L. Contovounesios
Subject: Re: Pattern matching on match-string groups #elisp #question
Date: Thu, 25 Feb 2021 14:55:58 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Ag Ibragimov <agzam.ibragimov@gmail.com> writes:

> Can someone please tell if it is possible to use pcase-let to dissect
> groups (parenthesized expressions) of match-string. Here's a dumb
> example:
>
> (let* ((s "lorem-foo-1-ipsum-bar-21_baz-42")
>       (_ (string-match "\\(foo-[0-9]+\\).*\\(bar-[0-9]+\\).*\\(baz-[0-9]+\\)" 
> s))
>       (group-1 (match-string 1 s))
>       (group-2 (match-string 2 s))
>       (group-3 (match-string 3 s)))
>  (format "%s-%s-%s" group-1 group-2 group-3)) ;; => "foo-1-bar-21-baz-42"

The closest equivalent pcase magic I can think of is:

  (pcase "lorem-foo-1-ipsum-bar-21_baz-42"
    ((rx (let group-1 "foo-" (+ num)) (* nonl)
         (let group-2 "bar-" (+ num)) (* nonl)
         (let group-3 "baz-" (+ num)))
     (format "%s-%s-%s" group-1 group-2 group-3)))
  ;; => "foo-1-bar-21-baz-42"

The same won't work with pcase-let, so it's either unsupported or a bug.

> Is there a more elegant way of retrieving these groups?

Personally I'm fine with either of the above, although I wouldn't
generally feel comfortable with using the pcase magic in code that
others work on, since there are usually more vanilla/common ways of
structuring the code.  Either way, I think it's a matter of taste.

See also the following recentish thread:
https://lists.gnu.org/r/emacs-devel/2020-12/msg00069.html

-- 
Basil



reply via email to

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