Negative Regular-Expression

I’ve got a text-file, and I want to delete all
of the lines which do not contain the sub-string “switch“.

Some regex-engines will allow the search-replace ^.*((?!switch)).*$,
with empty string.
Most of “gnu-based” engines like grep and sed will support that.

For those who won’t,
you can mimic it with a positive, two-steps replacement,
it will require match-memory/backtrack/back-trace support
($1 or \1 is commonly used to remember the first match).
V8/JavaScript and Scintilla-based editor will support that.

I’m using my modded-version of Notepad2, which supports a such search-replace.

first replacement is to “mark a good line”:
replace ^(.*switch.*)$ with #\1 (adds a # characters at line-start).

second replacement will remove bad-lines with (simple) single-character-NOT rule:
replace ^[^#].*$ with an empty-string (removes lines that do not start with #).

(and you may probably want to restore the original-line by removing the # prefix using last search-replace: replace ^# to empty-string)


Also, it is quite flexible,
say I now want to keep lines with “enum” in-them too,
the first stage is:
replace ^(.*switch.*)$ with #\1
and replace ^(.*enum.*)$ with #\1 (both cases will the # prefix).
and the second stage is the same:
replace all ^[^#].*$ lines with an empty-string
(again you probably want to remove the # prefix with replacing ^# with empty-string)


If you think a single-#-prefix as a “mark” is “too simple”,
you can use ### prefix instead and change the second-stage with replacing ^[^#][^#][^#].*$ with empty-line.

It is so it will be uncommon-enough to avoid false-positive replacements,
so if you’re lines are taken from BASH, shell-scripts, htaccess or APACHE ini-files,
where # is a common-character (and often used as a comment-prefix),
choose another character,
if you just use it in your editor, a Hebrew character such as א will be OK too! :]

The replacement by marking a content using an uncommon phrase,
is a widely-used technique in computer-science (formal-languages)
where “the universe” is a languages, defined by a range of
possible characters (referred to as the “languages ABC”).