elisp-新手练习-2

大家好,这是我对 An Introduction to Programming in Emacs Lisp 12.6 Exercises with ‘re-search-forward’ 所做的练习。

• Write a function to search for a regular expression that matches two or more blank lines in sequence.

(defun check-two ()
  (interactive)
  (if (not (re-search-forward "^[ \t]*$" nil t))
      (message "No blank lines."))
  (catch 'exit-condition
    (while (re-search-forward "^[ \t]*$" nil t)
      (forward-char)
      (if (re-search-forward "^[ \t]*$" nil t)
          (let ((number-of-blank-lines 0))
            (while (re-search-forward "^[ \t]*$" (1+ (point)) t)
              (progn (forward-char)
                     (setq number-of-blank-lines (1+ number-of-blank-lines))))
            (backward-char)
            (if (< 1 number-of-blank-lines)
                (progn (message "Numbers of blank lines are: %s" number-of-blank-lines)
                       (throw 'exit-condition number-of-blank-lines))))))))

虽然在运气好的时候,似乎能达到题目要求,但是正如大家所见,这个代码片 断写得像面条一样。

还请大家提供一些建议。

谢谢。

你确认过題目的要求了吗?我怎么覚得 “Write a function to search for a regular expression that matches two or more blank lines in sequence.” 是写一个匹配“能匹配二行以上空行的正则表达式”的 Lisp 函数,而不是让你去匹配二行以上空行呢

我也觉得题目理解有误

@LdBeth @seagle0128

谢谢大家。确实是我自己理解有问题。

这是基于新的理解方式的解:

(defun reg-test ()
  (re-search-forward "^\n\\{2,\\}") nil t)

学习了,审题很重要😂