[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: |
John Mastro |
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 19:46:42 -0700 |
> 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.
I don't think the "\\1"-style references are going to help you, since
you need to do math with the numbers first.
So what you need is `match-string', like I used in my answer to your
previous question. That will let you get the numbers and do what you
like with them.
Also, `replace-regexp-in-string' probably isn't what you want, since
you're operating on buffer text (as opposed to a string). For this
purpose, stick with search functions (like `re-search-forward' and
`looking-at') followed by `replace-match'.
This is what it would look like to essentially combine my answers from
yesterday and today:
(defvar number-triplet-regexp
"\\(^\\| \\)\\([0-9.]+\\) \\([0-9.]+\\) \\([0-9.]+\\)\\( \\|$\\)")
(defun something ()
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "BEGIN" nil t)
(insert "\n")
(delete-horizontal-space)
(while (looking-at number-triplet-regexp)
(let ((i (string-to-number (match-string 2)))
(j (string-to-number (match-string 3)))
(k (string-to-number (match-string 4))))
(replace-match (format "%s %s\n" (/ (+ i j) 2.0) (/ (+ j k) 2.0)))))
(when (looking-at "END")
(replace-match "Z")))))
(I pulled the regexp out to a defvar purely to combat line length; it
doesn't change anything about how it works).
--
john
Message not available