6

次の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 または を返すにはどうすればよい""ですか?

4

1 に答える 1

3

re-search-backward-lax-whitespaceオプションのnoerror引数があります。

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

エラーを通知しません。

より一般的なエラー処理には、ignore-errorsまたはを使用できますcondition-case。後者については、

Emacs Lisp でのエラー処理

于 2013-08-31T01:53:19.707 に答える