简体   繁体   中英

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

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

and it doesn't execute next when it is

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

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. 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 ! .

You can use \\s* instead of \\s*\\t*\\n* because \\s matches any whitespace.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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