From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.help Subject: Re: help with regexp function Date: Thu, 23 Nov 2017 16:20:07 +0100 Message-ID: <87wp2hqaxk.fsf@gmx.net> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1511450486 12754 195.159.176.226 (23 Nov 2017 15:21:26 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 23 Nov 2017 15:21:26 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Nov 23 16:21:23 2017 Return-path: Envelope-to: geh-help-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 1eHtJU-0002hD-Sg for geh-help-gnu-emacs@m.gmane.org; Thu, 23 Nov 2017 16:21:16 +0100 Original-Received: from localhost ([::1]:44847 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHtJc-0005wV-3K for geh-help-gnu-emacs@m.gmane.org; Thu, 23 Nov 2017 10:21:24 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHtIn-0005uo-4v for help-gnu-emacs@gnu.org; Thu, 23 Nov 2017 10:20:34 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHtIi-0005cc-A1 for help-gnu-emacs@gnu.org; Thu, 23 Nov 2017 10:20:33 -0500 Original-Received: from [195.159.176.226] (port=51598 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eHtIh-0005bF-Sr for help-gnu-emacs@gnu.org; Thu, 23 Nov 2017 10:20:28 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1eHtIV-0007co-Be for help-gnu-emacs@gnu.org; Thu, 23 Nov 2017 16:20:15 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 77 Original-X-Complaints-To: usenet@blaine.gmane.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:115070 Archived-At: On Wed, 22 Nov 2017 22:18:14 -0600 "B. T. Raven" wrote: [...] > On 11/22/2017 05:15, Stephen Berman wrote: >> On Tue, 21 Nov 2017 17:30:15 -0600 "B. T. Raven" wrote: >> >>> Dear Emacs gurus: >>> >>> I can perform this inteactive substitution CM-%: \(^[0-9]+ \)\(.+\) >>> -> \2 \1) in order to change a buffer line prefixed with a number >>> into one post-fixed with the same number but I can't figue out how >>> to do the same programatically to a whole region. I started with >>> this code: [...] >> >> Here's a pretty direct translation of the interactive substitution: > This works correctly but it isn't exactly what regexp-quote returns. Note that (regexp-quote "\(^[0-9]+ \)\(.+\)") returns this string: "(\\^\\[0-9]\\+ )(\\.\\+)", which matches only the literal string "(^[0-9]+ )(.+)", so it's not what you want. > Is there a function that produces "^\\([0-9]+\\) \\(.*\\)$" from > "\(^[0-9]+ \)\(.+\)" ? I don't know of any. But in order for the Lisp reader to recognize a backslash in a string as a backslash, you have to double it (because the backslash is used as the escape character is the Lisp read syntax). So you can just write your regexp as you would when using query-replace-regexp and then double each of the backslashes to use it in a Lisp program. > Did you replace .+ with .* just for greater generality? Yep. [...] >> I can't tell what the problem without seeing the code that causes it. >> Were you trying to treat the propertized string as a list because of >> the #(...) notation? > > No, I don't understand that notation. I started with other regexp > functions like query-replace-regexp but was getting type errors and > general confusion. I still can't tell how such errors arose, but it's probably not worth pursuing now. >>> ;; I have a function which is a black box to to me but it works in >>> the larger context I have it in. Does match-string do something like >>> this implicitly (casting a list as a string?) >> >> Not AFAIK. > > Here is the function I was talking about: > (defun reverse-string (str) > (apply #'string (nreverse (string-to-list str)))) > > It sounded like (append #'string ... was recasting a list to a string. ^^^^^^ apply I'm not sure it's helpful to think of it like that, since using a list is an artefact of the function definition here: `string' takes one or more characters but Emacs Lisp functions cannot return multiple values, only single values such as a list of characters. But you can dispense with that intermediate step: (defun reverse-string (str) (let ((l (length str)) (nstr "")) (dotimes (i l nstr) (setq nstr (concat (string (aref str i)) nstr))))) In fact, this is essentially how `nreverse' (and 'reverse') operate on strings (so there's no need for `reverse-string'). In any case, I don't see what this has to do with match-string. Steve Berman