Regular expression literal

How do I RegEx dyslexic .. what RegEx can you use to find each of the following lines - except for "LoginException"?

NullPointerException
LoginException
BooException
Abc123Exception

      

Edit: To be clear, I'm looking for these lines in my text / log.

+2


a source to share


3 answers


In general, if you want a regex to match anything other than a specific string or pattern, it is almost always easier to transform the meaning of the test. So instead of "heres a pattern that I hope matches anything other than XYZ", check if XYZ matches and throws positive results.

If you only want those three then use

$ egrep '(NullPointer | Boo | Abc123) Exception' input.log


You might be successful with a two-stage conveyor, for example

$ grep Exception input.log | grep -v LoginException
+2


a source


Assuming PCRE syntax (Perl-RegEx compatible) (i.e. grep -P

):

\b(?!LoginException\b)\w*Exception\b

      



Example:

echo "NullPointerException LoginException BooException Abc123Exception LoginFooException" |
grep -P '\b(?!LoginException\b)\w*Exception\b'

      

+4


a source


You can use negative lookahead :

(?!Login)\b\w+Exception

      

You can do this with Perl, for example:

perl -ne 'print if /(?!Login)\b\w+Exception/' < mylog.log

      

+2


a source







All Articles