all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: gnu ist <gnuist006@gmail.com>
To: John Mastro <john.b.mastro@gmail.com>,
	 "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>,
	Emanuel Berg <embe8573@student.uu.se>
Subject: Re: Tricky Regexp - How to insert a marker every 3rd number in a sequence that begins with a certain delimiter
Date: Sat, 6 Jun 2015 17:57:09 -0700	[thread overview]
Message-ID: <CAF=27p2qpcQnWRcb-J1sCTdu7f_vuPCURD8+L4f5wUNDLe-rFQ@mail.gmail.com> (raw)
In-Reply-To: <CAOj2CQSLKKxS1xT1nWLe3mP9XaypddXux7F=9tnT_jiMgN+CXA@mail.gmail.com>

On Saturday, June 6, 2015 at 10:44:55 AM UTC-7, gnui...@gmail.com wrote:
> tricky regexp
>
> How to insert a marker every 3rd number in a sequence that begins with a
certain delimiter, and ends with a certain delimiter and its length is a
multiple of three?
>
> I want to isolate sequences like this in a text and to work on them only.
>
> Given:-
>
> text
> text
> BEGIN N N N END
> text BEGIN N N N N N N END
> some text BEGIN N N N N N N N N N END
> text
> N N N N N N N
> text
>
> The sequences I want to work on start with BEGIN and end with END with
exact multiple of 3 B's in between with only single space. I want to place
a newline before every 3 B's. So the above text would transform to
>
> text
> text
> BEGIN
> N N N
> Z
> text BEGIN
> N N N
> N N N
> Z
> some text BEGIN
> N N N
> N N N
> N N N
> Z
> text
> N N N N N N N
> text
>
>
> More accurately, in a "BEGIN N N N N N N N N N END" type sequence, I want
to insert a \n before every N whose cardinality is 3n where n=0,1,2,... and
the first N has cardinality 0. I also want to insert a \n righ before such
a END.
>
>
> My efforts:
>
> (replace-regexp "BEGIN \\([0-9]\\) \\([0-9]\\) \\([0-9]\\) END" "BEGIN
\n\\1 \\2 \\3 \nEND")

Ok, your replies are quite helpful.

Now, lets focus on this part.

          (while (looking-at "\\(^\\| \\)N N N\\( \\|$\\)")
            (goto-char (match-end 0))
            (delete-horizontal-space t)
            (insert "\n"))


This successfully adds the newlines every three N's and is equivalent to
John Mastro's if I use replace-regexp-in-string and it allows me to refer
to the first three as \\1 \\2 \\3 while the outer re-search-forward as
(match-string 0).

A further problem is that I need to take the average of the first two N's
and last two N's and replace the first by the first average and last by the
second average leaving the middle N as it is.

Unfortunately, the problem is that within re-search-forward, replace-match
can reference tagged-groups as (match-string 2) etc but to do an eval for
replace-match and get the indefinite string "N N N ..." which is equivalent
to John Mastro's if I use replace-regexp-in-string.
but if you want to an arithmetic on this I am getting a problem. The
snippet is

(replace-match   (replace-regexp-in-string "\\([0-9.]+\\) \\([0-9.]+\\)
\\([0-9.]+\\)"
(concat "\n" (format "%s " (string-to-number "\\1")) "\\2" .....))

The second \\2 is correctly replaced by its string value, but the first by
two inverse transformations is not. \\2 is just inside a function (concat)
while \\1 is a second level.

The error is that the value of \\1 is just zero. If I replace \\1 by lets
say 6 then I get the correct answer.


  parent reply	other threads:[~2015-06-07  0:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-06 17:44 Tricky Regexp - How to insert a marker every 3rd number in a sequence that begins with a certain delimiter gnuist006
2015-06-06 18:15 ` Emanuel Berg
2015-06-06 19:08 ` John Mastro
     [not found]   ` <CAF=27p0sY6oPo=Hcj1uZn2-ZSb5tiLQDVV3YHjZdXQVZH2VSUA@mail.gmail.com>
     [not found]     ` <CAOj2CQSLKKxS1xT1nWLe3mP9XaypddXux7F=9tnT_jiMgN+CXA@mail.gmail.com>
2015-06-07  0:57       ` gnu ist [this message]
2015-06-07  2:46         ` John Mastro
     [not found] ` <mailman.4479.1433617759.904.help-gnu-emacs@gnu.org>
2015-06-06 22:14   ` Emanuel Berg

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAF=27p2qpcQnWRcb-J1sCTdu7f_vuPCURD8+L4f5wUNDLe-rFQ@mail.gmail.com' \
    --to=gnuist006@gmail.com \
    --cc=embe8573@student.uu.se \
    --cc=help-gnu-emacs@gnu.org \
    --cc=john.b.mastro@gmail.com \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.