Instead of erroring out from the hook, you apparently can use the variable kill-buffer-query-functions, which seems to be made specially for that.
(defun my/confirm-closing-buffer ()
(or (string-match-p "^ [*]" (buffer-name))
(y-or-n-p (format "Are you sure you want to kill buffer `%s'? " (buffer-name)))))
Use kill-buffer-hook. But you probably don't want to force confirmation when a temporary buffer ( *...) gets killed.
Add this or similar to your init file:
(defun foo ()
(unless (or (string-match-p "^ [*]" (buffer-name))
(format "Are you sure you want to kill buffer `%s'? "
Besides the options mentioned in phils answer, you can use shell-command-on-region for this. Select the region you'd like to 'filter' first. When prefixed with a universal argument C-u, the sent text gets replaced (you can read its docstring for more info).
I don't know your exact usecase, but generally swiper or helm-swoop provide similar and generally more ...
I want the text not matching the pattern to be gone
M-x keep-lines will delete lines not matching the pattern (i.e. only keep the lines which match). flush-lines does the opposite, deleting lines which do match the pattern.
M-x occur will open a separate buffer showing only the matching lines (which you can use like a compilation error buffer to navigate ...