From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Tino Calancha <tino.calancha@gmail.com>
Cc: 27659@debbugs.gnu.org,
Michael Heerdegen <michael_heerdegen@web.de>,
Philipp Stephani <p.stephani2@gmail.com>
Subject: bug#27659: 26.0.50; Add string-matched-text: string-match + match-string
Date: Fri, 21 Jul 2017 09:34:44 -0400 [thread overview]
Message-ID: <jwvd18uexjj.fsf-monnier+emacsbugs@gnu.org> (raw)
In-Reply-To: <alpine.DEB.2.20.1707212123470.28684@calancha-pc> (Tino Calancha's message of "Fri, 21 Jul 2017 21:29:08 +0900 (JST)")
>> This looks useful, but I think it would be even better to add it
>> as a pcase macro to be composable (see attached patch).
Hey, very nice. Please add it to rx.el.
[ But please change `backref-var` to just `backref` (you can distinguish
the two based on the type of the argument, I think). I guess one
could also argue that you could similarly rename the `let` to
`group-n`. ]
> Although, i must admit i am not fluent in `rx' syntaxis, i find your idea
> very nice.
If you prefer the standard/cryptic regexp syntax, I posted a similar
thingy in the past (see below).
This lets you do
(pcase "foo-123"
((re-match "[[:alpha:]]+-\\(?num:[0-9]+\\)")
num))
=> "123"
Maybe I should install it in pcase.el?
Stefan
(pcase-defmacro re-match (re)
"Matches a string if that string matches RE.
RE should be a regular expression (a string).
It can use the special syntax \\(?VAR: to bind a sub-match
to variable VAR. All other subgroups are treated as shy.
Multiple uses of this macro in a single `pcase' are not optimized
together, so don't expect lex-like performance. But in order for
such optimization to be possible in some distant future, back-references
are not supported."
(let ((start 0)
(last 0)
(new-re '())
(vars '())
(gn 0))
(while (string-match "\\\\(\\(?:\\?\\([-[:alnum:]]*\\):\\)?" re start)
(setq start (match-end 0))
(let ((beg (match-beginning 0))
(name (match-string 1 re)))
;; Skip false positives, either backslash-escaped or within [...].
(when (subregexp-context-p re start last)
(cond
((null name)
(push (concat (substring re last beg) "\\(?:") new-re))
((string-match "\\`[0-9]" name)
(error "Variable can't start with a digit: %S" name))
(t
(let* ((var (intern name))
(id (cdr (assq var vars))))
(unless id
(setq gn (1+ gn))
(setq id gn)
(push (cons var gn) vars))
(push (concat (substring re last beg) (format "\\(?%d:" id))
new-re))))
(setq last start))))
(push (substring re last) new-re)
(setq new-re (mapconcat #'identity (nreverse new-re) ""))
`(and (pred stringp)
(app (lambda (s)
(save-match-data
(when (string-match ,new-re s)
(vector ,@(mapcar (lambda (x) `(match-string ,(cdr x) s))
vars)))))
(,'\` [,@(mapcar (lambda (x) (list '\, (car x))) vars)])))))
next prev parent reply other threads:[~2017-07-21 13:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-12 6:13 bug#27659: 26.0.50; Add string-matched-text: string-match + match-string Tino Calancha
2017-07-20 0:54 ` Drew Adams
2017-07-20 1:19 ` Tino Calancha
2017-07-20 19:51 ` Philipp Stephani
2017-07-21 12:29 ` Tino Calancha
2017-07-21 13:34 ` Stefan Monnier [this message]
2017-07-21 14:08 ` Tino Calancha
2017-07-21 23:28 ` John Mastro
2017-07-22 2:02 ` Michael Heerdegen
2017-07-23 20:41 ` Philipp Stephani
2017-07-24 14:30 ` Stefan Monnier
2017-07-22 1:46 ` Michael Heerdegen
2017-07-23 20:45 ` Philipp Stephani
2017-07-23 21:39 ` 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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvd18uexjj.fsf-monnier+emacsbugs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=27659@debbugs.gnu.org \
--cc=michael_heerdegen@web.de \
--cc=p.stephani2@gmail.com \
--cc=tino.calancha@gmail.com \
/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.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.