在以下中,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-errors
或condition-case
。有关后者的信息,请参见Error Handling in Emacs Lisp
关于emacs - Emacs Lisp-如何尝试/捕获处理错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18543673/