[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: help with regexp function
From: |
B. T. Raven |
Subject: |
Re: help with regexp function |
Date: |
Wed, 22 Nov 2017 22:18:14 -0600 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 |
Thanks, Stephen and Kendall. Stephen's solution is exactly what I wanted
except for the close-paren after the line number. What I needed to know
about is the just discovered regexp-quote and re-builder functions. What
I was relying on was the replace-match docs at C-hf whose syntax is much
more complicated than your concatenation of match-strings 1 and 2.
Since my thinking about the problem was so tentative I won't try to
remediate my code but just go with what Stephen wrote. Kendall wrote:
"
re-search-forward signals an error if the search string isn't matched. I
don't know if that could explain the error you are seeing, but this
would prevent a non-local exit:
(cond
((re-search-forward "^[0-9]+ " nil t)
(setq num ...)
(print num))
(t
(goto-char end)))
"
On 11/22/2017 05:15, Stephen Berman wrote:
> On Tue, 21 Nov 2017 17:30:15 -0600 "B. T. Raven" <btraven@nihilo.net>
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.
Is there a function that produces "^\\([0-9]+\\) \\(.*\\)$" from
"\(^[0-9]+ \)\(.+\)" ? Did you replace .+ with .* just for greater
generality?
>
> (defun verse-num-move-beg-to-end (beg end)
> "Move int-string and following space from beginning of line to
> end of line throughout region."
> (interactive "r")
> (goto-char beg)
> (while (<= (point) end)
> (re-search-forward "^\\([0-9]+\\) \\(.*\\)$")
> (replace-match (concat (match-string 2) " " (match-string 1) ")"))))
[...]
> 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 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.
[...]
>>
>> Any help apppreciated.
>>
>> Ed
>
> Steve Berman
>
Thanks again,
Ed