Emacs, removing all lines that don't match?

I use the (query-replace-regexp "from" "to") expression regularly when making large changes in a text file.

I'd like a regular expression, if one exists, for removing all lines that don't match. For example, in a RedHat SPEC file for building a RPM I want to leave just the lines that begin with /^Patch/ in them (and delete all non-matching lines). Easy enough with grep -E '^Patch' but is there a way in Emacs?

I tried:

(query-replace-regexp "^\\(?!Patch\\)[^\r\n]*$" "")

to no avail (negative-lookahead appears unsupported).

Any ideas?


Solution 1:

Try M-x keep-lines ^Patch instead:

(keep-lines REGEXP &optional RSTART REND INTERACTIVE)

Delete all lines except those containing matches for REGEXP.

There is also the opposite command, M-x flush-lines, which removes lines matching a regexp.

Solution 2:

use M-x delete-non-matching-lines and M-x delete-matching-lines