正则表达式和 grep 异常匹配
我在日志文件中测试了我的正则表达式是否匹配异常:
正则表达式是:
.+Exception[^\n]+(\s+at.++)+
它适用于我粘贴在这里的几个案例,但当我将它与 grep 一起使用时则不起作用:
grep '.+Exception[^\n]+(\s+at.++)+' server.log
grep 是否需要一些额外的标志才能使其与正则表达式一起使用?
更新:
它不一定是正则表达式,我正在寻找任何可以打印异常的东西。
I tested my regex for matching exceptions in a log file :
Regex is :
.+Exception[^\n]+(\s+at.++)+
And it works for couple of cases I pasted here, but not when I'm using it with grep :
grep '.+Exception[^\n]+(\s+at.++)+' server.log
Does grep needs some extra flags to make it work wit regex ?
Update:
It doesn't have to be regex, I'm looking for anything that will print exceptions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
并非所有版本的
grep
都理解相同的语法。+
,这意味着它位于egrep
范围内。\s
,而大多数版本的grep
都不知道这一点。++
表示前面原子的所有格匹配,只有相当复杂的正则表达式引擎才能理解。您可以尝试非所有格匹配。但是,您不需要前导
.+
,因此您可以直接跳转到所需的字符串。另外,我不明白为什么您要使用[^\n]
因为这就是.
通常的含义,并且因为无论如何您已经在行模式下操作。如果您有
grep -P
,您可以尝试一下。我正在使用您的模式的更简单但等效的版本;你没有使用grep
的选项,它只给出精确的匹配,所以我假设你想要整个记录:但如果这不起作用,你总是可以拿出大枪:
并且如果你想要完全匹配,你可以使用
这应该给你足够的变化来玩。
我不明白为什么人们使用基于网络的“正则表达式”构建器,因为他们可以使用现有工具在命令行上执行相同的操作,因为这样他们就可以绝对确定他们设计的模式将与这些工具一起使用。
Not all versions of
grep
understand the same syntax.+
for 1 or more repeats, which means it is inegrep
territory.\s
for white space, which most versions ofgrep
are ignorant of.++
to mean a possessive match of the preceding atom, which only fairly sophisticated regex engines understand. You might try a non-possessive match.However, you don’t need a leading
.+
, so you can jump right to the string you want. Also, I don’t see why you would use[^\n]
since that’s what.
normally means, and because you’re operating in line mode already anyways.If you have
grep -P
, you might try that. I’m using a simpler but equivalent version of your pattern; you aren’t using an option togrep
that gives only the exact match, so I assume you want the whole record:But if that doesn’t work, you can always bring out the big guns:
And if you want just the exact match, you could use
That should give you enough variations to play with.
I don’t understand why people use web-based “regex” builders when they can just do the same on the command line with existing tools, since that way they can be absolutely certain the patterns they devise will work with those tools.
如果您想使用扩展正则表达式
-E -e
,则需要向其传递-e
选项。看看这个男人:man grep
You need to pass it the
-e <regex>
option and if you want to use the extended regex-E -e <regex>
. Take a look at the man:man grep
看起来您正在尝试查找类似于以下内容的行:
因此,首先,要使用正则表达式,您必须将
-e
与grep
一起使用,或者您可以运行egrep。接下来,您实际上不必在表达式的开头指定
.+
。通常最好尽量减少您要搜索的内容。如果必须在“Exception”之前至少有一个字符,则只需使用.
。另外,
\s
是一种类似 perl 的请求空间的方式。 grep 使用 POSIX 正则表达式,因此相当于[[:space:]]
。所以,我会使用:
这会以最少的混乱和大惊小怪获得你想要的东西。
It looks like you're trying to find lines that look something like:
So first, to use regular expressions, you have to use
-e
withgrep
, or you can just runegrep
.Next, you don't really have to specify the
.+
at the start of the expression. It's usually best to minimize what you're searching for. If it's imperative that there is at least one character before "Exception", then just use.
.Also,
\s
is a perl-ish way of asking for a space. grep uses POSIX regex, so the equivalent is[[:space:]]
.So, I would use:
This would get what you want with the least amount of muss and fuss.