all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: 27659@debbugs.gnu.org
Subject: bug#27659: 26.0.50; Add string-matched-text: string-match + match-string
Date: Wed, 12 Jul 2017 15:13:42 +0900	[thread overview]
Message-ID: <87fue2rxm1.fsf@calancha-pc> (raw)

Severity: wishlist

Just wondering if the following is of any interest:

(defun string-matched-text (regexp string num &optional start)
  ""
  (when (string-match regexp string start)
    (match-string num string)))
    
Then,

(let ((str "foo-123"))
  (when (string-match "[[:alpha:]]+-\\([0-9]+\\)" str)
    (match-string 1 str)))
=> "123"

is equivalent to:
(string-matched-text "[[:alpha:]]+-\\([0-9]+\\)" "foo-123" 1)
=> "123"

--8<-----------------------------cut here---------------start------------->8---
commit 65741b74d8999beaacd5093e128030a8635aff05
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Wed Jul 12 14:29:08 2017 +0900

    string-matched-text: New function
    
    * lisp/subr.el (string-matched-text): New defun.
    * doc/lispref/searching.texi (Simple Match Data): Update manual.
    * etc/NEWS: Announce it.

diff --git a/doc/lispref/searching.texi b/doc/lispref/searching.texi
index 67d4c22464..81bcad1740 100644
--- a/doc/lispref/searching.texi
+++ b/doc/lispref/searching.texi
@@ -1457,6 +1457,16 @@ Simple Match Data
 repetition that repeated zero times.
 @end defun
 
+@defun string-matched-text regexp string count &optional start
+This function returns, as a string, the text matched by @var{regexp}
+in @var{string}, or @code{nil} if there is no match.  The meaning
+of @var{count} is same as in @code{match-string}.  If @var{start}
+is non-@code{nil}, the search starts at that index in @var{string}.
+The behavior of this function is equivalent to
+@w{@code{(and (string-match @var{regexp} @var{string} @var{start})
+(match-string @var{count} @var{string}))}}.
+@end defun
+
 @defun match-string-no-properties count &optional in-string
 This function is like @code{match-string} except that the result
 has no text properties.
diff --git a/etc/NEWS b/etc/NEWS
index 68ebdb3c15..794edef9cd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1105,6 +1105,8 @@ break.
 \f
 * Lisp Changes in Emacs 26.1
 
+** New function 'string-matched-text'.
+
 ** New function 'seq-set-equal-p' to check if SEQUENCE1 and SEQUENCE2
 contain the same elements, regardless of the order.
 
diff --git a/lisp/subr.el b/lisp/subr.el
index a9edff6166..1482787842 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3635,6 +3635,15 @@ match-string
 	  (substring string (match-beginning num) (match-end num))
 	(buffer-substring (match-beginning num) (match-end num)))))
 
+(defun string-matched-text (regexp string num &optional start)
+  "Return string of text matched by REGEXP in STRING.
+NUM specifies which parenthesized expression in REGEXP.
+  Value is nil if NUMth pair didn’t match, or there were less than NUM pairs.
+Zero means the entire text matched by the whole regexp or whole string.
+If optional arg START is non-nil, then start search at that index in STRING."
+  (when (string-match regexp string start)
+    (match-string num string)))
+
 (defun match-string-no-properties (num &optional string)
   "Return string of text matched by last search, without text properties.
 NUM specifies which parenthesized expression in the last regexp.
--8<-----------------------------cut here---------------end--------------->8---

In GNU Emacs 26.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2017-07-11
Repository revision: d014a5e15c1110af77e7a96f06ccd0f0cafb099f





             reply	other threads:[~2017-07-12  6:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-12  6:13 Tino Calancha [this message]
2017-07-20  0:54 ` bug#27659: 26.0.50; Add string-matched-text: string-match + match-string 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
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=87fue2rxm1.fsf@calancha-pc \
    --to=tino.calancha@gmail.com \
    --cc=27659@debbugs.gnu.org \
    /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.