I need to replace below string:
(24.0000,28.0000) .. controls (27.3100,28.0000) and (29.9800,25.3100) .. (29.9800,22.0000)
to:
(12,14) .. controls (13.655,14) and (14.99,12.655) .. (14.99,11)
The rule is:
- coordinate x and y divided by 2.
- remove trailing zeros.
- other parts keep it as is.
I wish to do it in elisp:
#+BEGIN_SRC elisp :results output
(let (str x y (i 0))
(setq str "
(24.0000,28.0000) .. controls (27.3100,28.0000) and (29.9800,25.3100) .. (29.9800,22.0000)
")
(while (string-match "(\\([0-9.]+\\),\\([0-9.]+\\))" str i)
(setq i (match-end 0))
(setq x (/ (string-to-number (match-string 1 str) 10) 2))
(setq y (/ (string-to-number (match-string 2 str) 10) 2))
(print (format "%f,%f" x y))
))
#+END_SRC
#+RESULTS:
:
: "12.000000,14.000000"
:
: "13.655000,14.000000"
:
: "14.990000,12.655000"
:
: "14.990000,11.000000"
It can extract the numbers and do match on it, but how can I replace it within original string?
I am thinking about replace-regexp-in-string
but it not good to do match on it.
replace-regexp-in-string
- what was the problem? Your regex should match what you need, although it will match more than you need. Also note that the third argument to this function could be a function which performs arbitrary calculations to produce substituted string. – wvxvw Nov 16 '17 at 8:51%.3g
instead of%.3f
? – npostavs Nov 16 '17 at 23:40