I guess you want a two-pass system then, whereby you firstly collect/count identifiers, and secondly discard those which appeared more than once, and then highlight what's left. Here's a rough implementation... The initial regexp `"\\_<.+?\\_>"` and the subsequent `(regexp-opt unique 'symbols)` are connected, as the `'symbols` argument wraps `\_<` and `\_>` (being symbol start/end delimiters) around the result. This ensures that the latter search will only match instances of the original search. If you weren't matching symbols, you'd need to change both patterns, with appropriate care taken. ```el (defun my-highlight-unique-symbols () "In each block, highlight all symbols which occur exactly once." (interactive) (save-excursion (goto-char (point-min)) (let ((case-fold-search nil) (blockstart (point))) ;; Establish the next block. (while (and (not (eobp)) (or (search-forward "\n\n\n" nil t) (goto-char (point-max)))) (let ((blockend (point))) (goto-char blockstart) ;; Highlight unique identifiers in this block. (let ((identifiers (obarray-make)) unique) (save-excursion (while (re-search-forward "\\_<.+?\\_>" blockend t) (let* ((sym (intern (match-string 0) identifiers))) ;; Count the occurrences of each match. (set sym (1+ (or (and (boundp sym) (symbol-value sym)) 0)))))) ;; Extract the unique instances. (obarray-map (lambda (sym) (when (eql (symbol-value sym) 1) (push (symbol-name sym) unique))) identifiers) ;; Highlight the unique instances. (let ((regexp (regexp-opt unique 'symbols))) (save-excursion (with-silent-modifications (while (re-search-forward regexp blockend t) (put-text-property (match-beginning 0) (match-end 0) 'face 'highlight))))) (setq blockstart blockend))))))) ``` Which you can run with <kbd>M-x</kbd> `my-highlight-unique-symbols` You will need to disable `font-lock-mode` in the buffer first, or else use `'font-lock-face` in place of `'face` at the end of that code. I'd recommend that, except the other font-lock rules will clobber this in some cases (observed with matches within comments not appearing to be highlighted), and I'm not familiar enough with font-lock to know how to specify an overriding/priority face. You can remove all `face` properties in the buffer with: <kbd>M-:</kbd> `(remove-text-properties (point-min) (point-max) '(face nil))`