help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Tricky Regexp - How to insert a marker every 3rd number in a sequenc


From: Emanuel Berg
Subject: Re: Tricky Regexp - How to insert a marker every 3rd number in a sequence that begins with a certain delimiter
Date: Sun, 07 Jun 2015 00:14:37 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

John Mastro <john.b.mastro@gmail.com> writes:

> Is there a particular reason you want/need to use
> a (single) regular expression? It doesn't seem like
> a good fit to me. Unless you're somehow restricted
> to a single `replace-regexp', you may as well use
> more of Emacs's toolbox.
>
> Here's some quick-and-dirty Lisp (which, of course,
> uses regular expressions)

That's what I meant as well but you put it better.
You (the OP) should use BOTH Lisp and regexps!

> that works on your example but would need more work
> and refinement to serve your general purpose.

Are you sure there is one? :)

The only thing that might not be immediately obvious
from the code is the use of `re-search-forward' and
then `match-end'. re-search-forward with NOERROR (3rd
argument) supplied as t will return nil if there is no
hit. That will cause the loop to terminate.

While there are hits, match-end will return the
"position of end of text matched by last search", but
this isn't from the re-search-forward search but from
the `looking-at' "search".

Then the same mechanisms are at work with
`replace-match'.

And this general method is a good one! Search for
a regexp, then examine it (an easy fetch as the search
hit is stored), then insert or operate on it to
produce the desired output. Repeat until done.

Just add water!

-- 
underground experts united
http://user.it.uu.se/~embe8573


reply via email to

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