Emacs Regexp Note

scinart posted @ 2013年8月16日 12:34 in elisp , 1770 阅读

How to build your regexp: http://ergoemacs.org/emacs/emacs_regex.html

more details: http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexps.html

find regex in string: http://www.gnu.org/software/emacs/manual/html_node/elisp/Simple-Match-Data.html

replace it: (replace-regexp-in-string (regexp-quote SUBSTRING) STRING)

 

One Example: 

(defun eval-string (string)
  "eval this string without side effect"
  (eval (read string) t))
(defun superstring (string)

  "eval #{expression} in string like ruby"
  (while (string-match "#{[^}]*?}" string)
    (let* ((b (match-beginning 0))
             (e (match-end 0))
             (whole (substring string b e))
             (exp (substring string (+ 2 b) (- e 1))))
      (setq string
        (replace-regexp-in-string (regexp-quote whole)
                      (format "%S" (eval-string exp)) string))))
  string)

(superstring "the sum from 1 to 5 is #{(reduce #'+ '(1 2 3 4 5))}.")
;; -> "the sum from 1 to 5 is 15."


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter