From: "B. T. Raven" <btraven@nihilo.net>
To: help-gnu-emacs@gnu.org
Subject: Re: help with regexp function
Date: Fri, 24 Nov 2017 08:46:59 -0600 [thread overview]
Message-ID: <ov9bd00381@news4.newsguy.com> (raw)
In-Reply-To: <mailman.4372.1511450434.27995.help-gnu-emacs@gnu.org>
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
>
>
next prev parent reply other threads:[~2017-11-24 14:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-21 23:30 help with regexp function B. T. Raven
2017-11-22 11:15 ` Stephen Berman
2017-11-22 17:05 ` Kendall Shaw
[not found] ` <mailman.4298.1511349362.27995.help-gnu-emacs@gnu.org>
2017-11-23 4:18 ` B. T. Raven
2017-11-23 15:20 ` Stephen Berman
[not found] ` <mailman.4372.1511450434.27995.help-gnu-emacs@gnu.org>
2017-11-24 14:46 ` B. T. Raven [this message]
2017-11-24 16:28 ` Stephen Berman
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ov9bd00381@news4.newsguy.com \
--to=btraven@nihilo.net \
--cc=help-gnu-emacs@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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).