deleting lines in ex

Hello, im using ex to manipulate some text. Im trying to delete all the lines except those on which a certain regex can be found.

the important part of the script:

ex temp << 'HERE'
g/regex/p
HERE

this command prints the lines I want to end up with, but it doesnt delete the others. is there a way to solve this?
something like
g/!regex/d
or
anything else?
thx alot!

v/regex/d
1,$p

but it is simpler to

grep 'regex' file | tee file2
1 Like

! thanks :smiley: