\nFind what: ^(?!.*hede).*\\s?
\nReplace with: nothing
Explanation:
\n ^ # start of a line\n (?!) # a Negative Lookahead \n . # matches any character (except for line terminators)\n * # matches the previous token between zero and unlimited times,\n hede # matches the characters hede literally\n \\s # matches any whitespace character (equivalent to [\\r\\n\\t\\f\\v ])\n ? # matches the previous token between zero and one times,\n
\n","author":{"@type":"Person","name":"Haji Rahmatullah"},"upvoteCount":1}}}Reputation: 866
So, I know from this question how to find all the lines that don't contain a specific string. But it leaves a lot of empty newlines when I use it, for example, in a text editor substitution (Notepad++, Sublime, etc).
Is there a way to also remove the empty lines left behind by the substitution in the same regex or, as it's mentioned on the accepted answer, "this is not something regex ... should do"?
Example, based on the example from that question:
aahoho
bbhihi
cchaha
sshede
ddhudu
wwhada
hede
eehidi
sshede
hede
Let's try this again: what I want is a way to use regex replace to remove everything that does not contain hede
on the text editor. If I try .*hede.*
it will find all hede
:
But it will not remove. On a short file, this is easy to do manually, but the idea here is to replace on a larger file, with over 1000+ lines, but that would contain anywhere between 20-50 lines with the desired string.
If I use ^((?!hede).)*$
and replace it with nothing, I end up with empty lines:
I thought it was a simple question, for people with a better understanding of regex than me: can a single regex replace also remove those empty lines left behind?
Upvotes: 0
Views: 352
Reputation: 430
An alternative try
Find what: ^(?!.*hede).*\s?
Replace with: nothing
Explanation:
^ # start of a line
(?!) # a Negative Lookahead
. # matches any character (except for line terminators)
* # matches the previous token between zero and unlimited times,
hede # matches the characters hede literally
\s # matches any whitespace character (equivalent to [\r\n\t\f\v ])
? # matches the previous token between zero and one times,
Upvotes: 1
Reputation: 5459
Have you tried:
.*hede.*
I don't know why you are doing an inverse search for this.
You can use sed
like:
sed -e '/.*hede.*/!d' input.txt
Upvotes: 0
Reputation: 91518
Using Notepad++.
^((?!hede).)*(?:\R|\z)
LEAVE EMPTY
. matches newline
Explanation:
^ # beginning of line
((?!hede).)* # tempered greedy token, make sure we haven't hede in the line
(?:\R|\z) # non capture group, any kind of line break OR end of file
Screenshot (before):
Screenshot (after):
Upvotes: 1