在以下中,defun 中...

(defun re-backward-match-group (rexp &optional n)
  "Grab the previous matches of regexp and return the contents of
  the n match group (first group match if no n arg is specified)"
  (save-excursion
  (unless n
    (setq n 1))
  (when (numberp n)
    (when (re-search-backward-lax-whitespace rexp)
      (when  (= (+ 2 (* n 2)) (length (match-data)))
        (match-string-no-properties n))))))

如果找不到匹配项,则re-search-backward-lax-whitespace引发错误

我将如何捕获该错误并返回nil或""

最佳答案

re-search-backward-lax-whitespace具有可选的noerror参数。

(re-search-backward-lax-whitespace rexp nil t)

不会表示错误。

要进行更一般的错误处理,可以使用ignore-errorscondition-case。有关后者的信息,请参见

Error Handling in Emacs Lisp

关于emacs - Emacs Lisp-如何尝试/捕获处理错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18543673/

10-14 14:16
查看更多