2016-03-24

Tranlstaion with patterns (Emacs Lisp)

I have following practice problems. I would like to translate them to Japanese and German.
  • The $200$ is the $\\green{\\text{whole}}$. We are trying to find the $\\pink{\\text{part}}$ that makes up $11\\%$ of it:
  • The $25$ is the $\\green{\\text{whole}}$. We are trying to find the $\\pink{\\text{part}}$ that makes up $12\\%$ of it:
  • ...
You might notice these problems differs only inside $$. So, I can replace them with the following regexp by emacs's query-replace-regexp function.
  • The $\(.*\)$ is the $\(.*\)$. We are trying to find the $\(.*\)$ that makes up $\(.*\)$ of it:
  • $\1$ は $\2$ です.私達はその $\4$ となるだけの $\3$ を探しています:
You might also notice, you can see a pattern from this sentence
  • The $200$ is the $\\green{\\text{whole}}$. We are trying to find the $\\pink{\\text{part}}$ that makes up $11\\%$ of it:
to
  • The $\(.*\)$ is the $\(.*\)$. We are trying to find the $\(.*\)$ that makes up $\(.*\)$ of it:
I thought this is done by the following function:
  • (query-replace-regexp "\\$[^\\$]+\\$" "$\\\\(.*\\\\)$")
However, "\\$[^\\$]+\\$" doesn't match $\\green{\\text{whole}}$.

I don't know why this doesn't work, so I wrote the following small program. This works well.

(let ((finishp nil))
  (while (null finishp)
    (progn
      (setq match-start (search-forward "$" nil t))
      (setq match-end   (search-forward "$" nil t))
      (setq finishp (or (null match-start) (null match-end)))
      (if (null finishp)
          (progn
            (kill-region match-start match-end)
            (insert "\\(.*\\)$"))))))

But I am puzzled why this regexp ("\\$[^\\$]+\\$") doesn't match. Can anyone explain me that?

No comments: