(setq regex-1 ".*?mul(\\([[:digit:]]\\{1,3\\}\\),\\([[:digit:]]\\{1,3\\}\\))") (setq regex-2-1 ".*?\\(mul([[:digit:]]\\{1,3\\},[[:digit:]]\\{1,3\\})\\|don't()\\|do()\\)") (defun try-regex-part1 () (replace-regexp regex-1 "\n(* \\1 \\2);")) (defun try-regex-part2 () (interactive) (goto-char (point-min)) (replace-regexp regex-2-1 "\n\\1") ;remove most of the broken code (kill-line) ;remove the rest of the broken code (goto-char (point-min)) (replace-regexp "do()" "do") (goto-char (point-min)) (replace-regexp "don't()" "dont") (goto-char (point-min)) (replace-regexp regex-1 "(* \\1 \\2)") (goto-char (point-min)) (insert "(") (goto-char (point-max)) (insert ")") ) ;; the above turns the input into some pseudo lisp code. the code is then executed by the sexp below (-reduce-from (lambda (i e) (cond ((eq e 'do) `(t ,(cadr i))) ((eq e 'dont) `(nil ,(cadr i))) (t (if (car i) `(t ,(+ (cadr i) (eval e))) `(t ,(cadr i)))))) '(t 0) code)) ; this is quite horrible, and probably there is a more efficient way