4

I have a Json file containing a quite a few coordinates like {x: 400, y:200} And I want to change the numbers for the y values in something like y:y_default + 120. (y_default = 80). Is there a quick way to achieve this?

5

You can do that interactively by query-replace-regexp with embedded Elisp in the replacement string.

  1. Go to the beginning of the file.
  2. Type M-x query-replace-regexp RET.
  3. Input {x: \([0-9]+\), y:\([0-9]+\)} as search regexp.
  4. Input {x: \1, y:y_default + \,(- (read \2) 80)} as replacement string.
  5. Answer the queries of query-replace-regexp as appropriate.

The escape sequence \,(...) evaluates the sexp behind the \, and replaces itself with the result. You can use the usual references to subgroups as \1 and \2 in the sexp. They are interpolated as strings.

| improve this answer | |
  • Thanks for this answer! An extra reason to pickup lisp again. What was your reason to use (read str) above (string-to-number str)? – dr jerry Apr 16 at 19:44
  • @drjerry \1 is interpolated as string but we need it as number for the minus operator. The read employs the Elisp reader to transform the string into a number. You could also use string-to-number. – Tobias Apr 16 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.