I've written the following function to go from an error line to the next one:

(defconst mylog-mode-error-regexp
  "\\(\\[ERROR\\]\\|rule did fail\\)"
  "Regexp to recognize errors in my log file.")

(defun mylog-mode-next-error ()
  "Go to next error."
  (let ((case-fold-search nil))
    (search-forward-regexp mylog-mode-error-regexp)
    (narrow-to-region 1 (line-end-position))
    (setq current-count (mylog-mode-count-errors))
    (message "Error %d/%d" current-count (mylog-mode-count-errors))))

I'd like to skip some lines, the ones matching:

[ERROR][2018-05-29 07:39:25,149][Authenticated] - [Page [/login.html] is not allowed]

… as those errors aren't real errors -- which I have to debug.

How could I do that?

  • 1
    Don't count them, in your mylog-mode-count-errors?
    – Drew
    May 29, 2018 at 15:33
  • @Drew, that's another valid point, they should not be counted at all. The thing is I can't describe what must be counted, I only can describe what must NOT be counted (the line above). I should count all matches, and then substract the specific count that matches the above line, then? May 30, 2018 at 7:42

1 Answer 1


Obviously the trivial option is to change your regexp so that only matches things you want it to match.

I believe you're asking how to test the match results, though. As with any search, (search-forward-regexp mylog-mode-error-regexp) produces match data which you can easily use to perform further processing on what the search found. Exactly how you do that is up to you, and you should read C-hig (elisp)Match Data

Here's one possible approach, which keeps searching until (this-match-is-valid) is true.

(while (and (search-forward-regexp mylog-mode-error-regexp)
                (narrow-to-region (match-beginning 0) (match-end 0))
                (goto-char (point-min))
                (not (this-match-is-valid))))))
;; do things with the valid match
  • IIUC, the matched data here will be the [ERROR] keyword only. How can I write the this-match-is-valid function to see if it matches some regexp? There must be a function, but I don't find it. Regarding the narrowing, I guess I would narrow between beginning of line and end of line, so that's OK. Only the matching part is missing now. Thanks for your info! May 30, 2018 at 7:46
  • You'll probably find the looking-at function useful.
    – phils
    May 30, 2018 at 8:38
  • And of course if the narrowing isn't useful to you, don't do it. There's no need to narrow here -- it's just an example of something you could do to assist with analysing the match.
    – phils
    May 30, 2018 at 9:27
  • looking-at is certainly the function I was missing. And, of course, I agree about the narrowing… Thanks a lot! May 30, 2018 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.