开发者

Regular expression literal

As I'm RegEx dyslexic.. what RegEx can you use to find each of the following strings - with the exception of "LoginException"?

NullPointerException
LoginException
BooException
Abc123Exception

Edit: To be clear, I'm looking for these 开发者_JS百科strings in a text/log file.


Assuming PCRE (Perl-Compatible RegEx)-like syntax (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'


In general, when you want a regular expression that matches anything except a particular string or pattern, it's almost always more straightforward to invert the sense of the test. So instead of “here’s a pattern I hope matches anything except XYZ,” check whether XYZ matches and throw away the positives.

If you want only those three, use

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

You could probably get away with a two-stage pipeline, e.g.,

$ grep Exception input.log | grep -v LoginException


You can use a negative lookahead:

(?!Login)\b\w+Exception

You can do it for example with Perl:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜