From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#27659: 26.0.50; Add string-matched-text: string-match + match-string Date: Fri, 21 Jul 2017 09:34:44 -0400 Message-ID: References: <87fue2rxm1.fsf@calancha-pc> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1500644121 7665 195.159.176.226 (21 Jul 2017 13:35:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 21 Jul 2017 13:35:21 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: 27659@debbugs.gnu.org, Michael Heerdegen , Philipp Stephani To: Tino Calancha Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Jul 21 15:35:15 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dYY5E-0001MD-L3 for geb-bug-gnu-emacs@m.gmane.org; Fri, 21 Jul 2017 15:35:08 +0200 Original-Received: from localhost ([::1]:43246 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dYY5K-0005UY-9U for geb-bug-gnu-emacs@m.gmane.org; Fri, 21 Jul 2017 09:35:14 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40969) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dYY5D-0005SE-4T for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2017 09:35:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dYY58-0002rJ-7i for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2017 09:35:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:47806) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dYY58-0002rF-4a for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2017 09:35:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dYY57-0006Zq-Vz for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2017 09:35:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 21 Jul 2017 13:35:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 27659 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: wontfix Original-Received: via spool by 27659-submit@debbugs.gnu.org id=B27659.150064409225265 (code B ref 27659); Fri, 21 Jul 2017 13:35:01 +0000 Original-Received: (at 27659) by debbugs.gnu.org; 21 Jul 2017 13:34:52 +0000 Original-Received: from localhost ([127.0.0.1]:50483 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dYY4y-0006ZR-E2 for submit@debbugs.gnu.org; Fri, 21 Jul 2017 09:34:52 -0400 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.181]:51314) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dYY4w-0006ZD-G9 for 27659@debbugs.gnu.org; Fri, 21 Jul 2017 09:34:50 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0A+CAAXAnJZ/9vnr2xTCRwBAQQBAQoBAYNahSCLV5BzLAGXaYVBBAICg35EFAECAQEBAQEBAWsoQhABhEYBBAFWIwULCzQSFBgNJIo6CLBviyMBAQEBBgImgyiIUoRDJ4V0AQSJdIZpjm+LFYsShVCDRIcWlB+BRDYhgQoxIQgwSYcyJINRhmwBAQE X-IPAS-Result: A0A+CAAXAnJZ/9vnr2xTCRwBAQQBAQoBAYNahSCLV5BzLAGXaYVBBAICg35EFAECAQEBAQEBAWsoQhABhEYBBAFWIwULCzQSFBgNJIo6CLBviyMBAQEBBgImgyiIUoRDJ4V0AQSJdIZpjm+LFYsShVCDRIcWlB+BRDYhgQoxIQgwSYcyJINRhmwBAQE X-IronPort-AV: E=Sophos;i="5.40,390,1496116800"; d="scan'208";a="320921771" Original-Received: from 108-175-231-219.dsl.teksavvy.com (HELO pastel.home) ([108.175.231.219]) by smtp.teksavvy.com with ESMTP; 21 Jul 2017 09:34:44 -0400 Original-Received: by pastel.home (Postfix, from userid 20848) id 967DB641C1; Fri, 21 Jul 2017 09:34:44 -0400 (EDT) In-Reply-To: (Tino Calancha's message of "Fri, 21 Jul 2017 21:29:08 +0900 (JST)") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:134828 Archived-At: >> This looks useful, but I think it would be even better to add it >> as a pcase macro to be composable (see attached patch).=A0 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)) =3D> "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)])))))