Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex with negative matching (ie, find string that _doesn't_ match regex)

Tags:

regex

vim

vi

I have a log file with the string "ERROR" on some lines. I want to delete every line that doesn't have ERROR so that I can see just what needs fixing. I was going to do something like the following in vim:

%s/!(ERROR)//

to replace non-error lines with an empty string.

I do not believe that standard regexes can do this, but maybe I'm wrong...

like image 476
Alexander Bird Avatar asked Jul 22 '09 00:07

Alexander Bird


People also ask

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

How do you use negation in regex?

Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.

How do you check if a regex matches a string?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.


1 Answers

Use the :g! command to delete every line that doesn't match.

:g!/ERROR/d 
like image 147
too much php Avatar answered Sep 27 '22 19:09

too much php