[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie regexp question

From: Paul Cohen
Subject: Re: Newbie regexp question
Date: Thu, 31 Oct 2002 13:56:42 GMT

Hi all,

Thanks to everyone who has been kind to take time to answer my question! Jay's 
answer is definitely closest to solving my problem.

"Bingham, Jay" wrote:

> After seeing Mike and Friedrich's exchange on your question I decided to 
> create my own solution, a general purpose function that allows the user to 
> specify the start and end as regular expressions and supply a replacement for 
> the text between them, and optionally by giving a numeric prefix argument to 
> the function force it to replace the tags as well.


> In the process I noticed that Mike's function has a logic flaw.  It will 
> never find the case of a missing end tag and instead deletes that final start 
> tag.


> Here is my function if you want it.

Yes I do! :-)

> (defun replace-between-regexp (start-re end-re repl-str &optional incl)
>   "Replace the text between two regular expressions supplied as arguments.
> With a numeric argument the regular expressions are included.
> When called non interactively incl should be nil for non-inclusion and
> non-nil for inclusion."
>   (interactive "sStart regexp: \nsEnd regexp: \nsReplace between %s and %s 
> with: \nP")
>   (while (re-search-forward start-re nil t)
>     (let ((beg (if incl (match-beginning 0) (match-end 0)))
>           (end
>            (progn
>              (if (re-search-forward end-re nil t)
>                  (if incl (match-end 0) (match-beginning 0))
>                nil))))
>       (if (not end)
>           (error "Unmatched \"%s\" sequence at position %d" start-re beg)
>         (delete-region beg end)
>         (insert repl-str)))))

I have few comments/questions.

I tried the above function in my *scratch* buffer by writing it and then adding 
the following lines (with line numbers!):

19. (replace-between-regexp "<!--Test-->" "<!--End of Test-->" "" 1)
21. Pub
22. <!--Test-->
23. Foo
24. <!--End of Test-->
25. Bar

I then evaluated the function with C-j. This resulted in:

19. (replace-between-regexp "<!--Test-->" "<!--End of Test-->" "" 1)
22. Pub
23. nil
25. Bar

With the cursor on line 24. My comments/questions are:

1) I understand that the "nil" on line 23 comes from the value of the last item 
in the function list, in this case "(insert repl-str)". But there is also a 
newline character is inserted after "nil". But I don't want either the "nil" or 
the extra newline character!

2) Line 21 containing "pub" is moved forward to line 22. I guess this is just 
because I did C-j at the end of line 19 or?

3) It would be nice if the cursor would return to its original position after 
running the command. I tried adding the "save-excursion" command after the 
"interactive" line in the function but it didn't work. The cursor still ended 
up on line 24.

4) The idea to solve my problem with a lisp function is neat and I guess there 
are many situations where one would like to add ones own special purpose 
functions to Emacs. My question is: where is the suitable place to put them? In 
my .emacs file or in a separate file. What are the conventions?

Thanks again for the help!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]