From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: "B. T. Raven" Newsgroups: gmane.emacs.help Subject: Re: help with regexp function Date: Fri, 24 Nov 2017 08:46:59 -0600 Organization: NewsGuy - Unlimited Usenet $23.95 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1511535048 11042 195.159.176.226 (24 Nov 2017 14:50:48 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 24 Nov 2017 14:50:48 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Nov 24 15:50:45 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 1eIFJN-0002Aj-DD for geh-help-gnu-emacs@m.gmane.org; Fri, 24 Nov 2017 15:50:37 +0100 Original-Received: from localhost ([::1]:49662 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eIFJU-0001P4-Rx for geh-help-gnu-emacs@m.gmane.org; Fri, 24 Nov 2017 09:50:44 -0500 X-Received: by 10.36.176.72 with SMTP id b8mr9016774itj.35.1511534871974; Fri, 24 Nov 2017 06:47:51 -0800 (PST) Original-Path: usenet.stanford.edu!d140no5362898itd.0!news-out.google.com!193ni456iti.0!nntp.google.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!news4 Original-Newsgroups: gnu.emacs.help Original-Lines: 81 Original-NNTP-Posting-Host: pa937a9dfb20586bb086d98b3bd047721887d868cb01c4903.newsdawg.com In-Reply-To: X-Received-Bytes: 3670 X-Received-Body-CRC: 1257817916 Content-Language: en-US Original-Xref: usenet.stanford.edu gnu.emacs.help:220963 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:115074 Archived-At: On 11/23/2017 09:20, Stephen Berman wrote: >>> 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. Okay, thanks. > >> Did you replace .+ with .* just for greater generality? > > Yep. > >> >> 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 Yes, because I tried to type rather than copypaste. I was distracted by the # (what does it mean?) > > 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. That looks like it may be faster than what I have now. Is it? Everything is in C except dotimes and string-to-list according to the function docs. Thanks again. > > Steve Berman > >