简体   繁体   English

Perl:regx模式匹配

[英]Perl: regx pattern matching

Whenever i find the word ".abc.corp:" in a line on file, i would like to exclude those lines: 每当我在文件中的一行中找到单词“ .abc.corp:”时,我都希望排除这些行:

Example Line: 示例行:

kubernte-fileserver-NN.abc.corp:/srv/export/storage/nsp_updates   1231231  123112  123123  89%  /devops

can someone help me to find out the correct regex pattern: 有人可以帮助我找出正确的正则表达式模式:

im trying out with below pattern match: unable to figure it out 我正在尝试以下模式匹配:无法弄清楚

/^(.*(?!\.abc\.corp).*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+([\/\w\d\.-]+)$/

I'm confused with subpattern group $1 with negative look around! 我对子模式组$ 1感到困惑,并带有负面的目光!

By "exclude it", I assume you want to exclude the entire line. 通过“排除”,我假设您要排除整个行。

Your try will not exclude anything, because here Perl can always find some point in the share path to split it, where the split point is not immediately followed by .abc.corp , like if it splits it: 您的尝试不会排除任何内容,因为Perl始终可以在共享路径中找到某个要拆分的点,该拆分点不会紧跟在.abc.corp ,就像将其拆分一样:

kubernte-fileserver-N
N.abc.corp:/srv/export/storage/nsp_updates

or (as it's actually going to do) just consume everything by the first .* , with nothing left for the second one. 或者(实际上是这样做的)只是在第一个.*消耗了所有内容,而第二个则没有任何剩余。

I'd instead first try to match the string you're trying to avoid, and failing to do so, proceed with the actual handling: 相反,我会首先尝试匹配要避免的字符串,但如果不这样做,请继续实际处理:

if (/^\S+\.abc\.corp:/) {
    # SKIP
}
elsif (/^(.*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+([\/\w\d\.-]+)$/) {
    ...
}

Besides actually working, this makes the code much more readable. 除了实际工作,这使得代码易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM