简体   繁体   English

Perl正则表达式不匹配字符串操作

[英]Perl regex not-match-strings operation

What I have so far: 到目前为止,我有:

Here is the regex I created 这是我创建的正则表达式

 if ( /PRINT\((\s*\n*\t*)[A-Z]+_[A-Z]*(_*)(DBG|NOT|UNC)/  ) { next; }

Currently it executes next when it detects 当前,它在检测到时执行next

PRINT(ABC_XYZ_DBG...
PRINT(ABC_XYZ_NOT...
PRINT(ABC_UNC...

and it doesn't execute next when it is 并且它next执行时不会执行

PRINT(ABC_XYZ_ERR...
PRINT(ABC_XYZ_WRN...
PRINT(ABC_ERR...

I want to change it to: 我想将其更改为:

I want to modify it so it will execute next for everything other than _ERR or _WRN 我想修改它,所以它会执行next比其他一切_ERR_WRN

PRINT(ABC_XYZ_ERR...
PRINT(ABC_XYZ_WRN...
PRINT(ABC_ERR...

I tried the following but it didn't match anything 我尝试了以下内容,但没有任何内容

        my $ERR = qr/ERR/;
        my $WRN = qr/WRN/;
 if ( /PRINT\((\s*\n*\t*)[A-Z]+_[A-Z]*(_*)(?!$ERR|$WRN)/  ) { next; }

I am making some mistake in the not-match (?!$ERR) operator but I don't know how to correct it. 我在不匹配(?!$ERR)运算符中犯了一些错误,但我不知道如何纠正它。 I appreciate your inputs in advance. 非常感谢您的投入。

next unless ( /PRINT\((\s*\n*\t*)[A-Z]+_[A-Z]*(_*)(ERR|WRN)/ );

You can match (ERR|WRN) instead of (DBG|NOT|UNC) and then negate it with ! 您可以匹配(ERR|WRN)而不是(DBG|NOT|UNC) ,然后使用! .

You can use \\s* instead of \\s*\\t*\\n* because \\s matches any whitespace. 您可以使用\\s*代替\\s*\\t*\\n*因为\\s匹配任何空格。

if ( ! /PRINT\((\s*)[A-Z]+_[A-Z]*(_*)(ERR|WRN)/ ) { next; }

Input: 输入:

PRINT(ABC_XYZ_DBG...
PRINT(ABC_XYZ_NOT...
PRINT(ABC_UNC...
PRINT(ABC_XYZ_ERR...
PRINT(ABC_XYZ_WRN...
PRINT(ABC_ERR...

One-liner: 一内胆:

perl -nle 'if ( ! /PRINT\((\s*)[A-Z]+_[A-Z]*(_*)(ERR|WRN)/  ) { next; } print' input.txt

Output: 输出:

PRINT(ABC_XYZ_ERR...
PRINT(ABC_XYZ_WRN...
PRINT(ABC_ERR...

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

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