Perl正则表达式匹配字符串没有结束的东西

Chr*_*ris 1 regex perl negative-lookahead

什么正则表达式会正确匹配这个?我想识别不以特定文本结尾的字符串(_array).我试图使用负向前瞻但无法使其正常工作.(注意显而易见的答案是反向(m {_array $}),但有一个原因我不想这样做).TIA

 use strict;
 use warnings;
 while(<DATA>) {
    #
    ## If the string does not end with '_array' print No, otherwise print Yes
    m{(?!_array)$} ? print "No  = " : print "Yes = ";
    print;
 }
 __DATA__
 chris
 hello_world_array
 another_example_array
 not_this_one
 hello_world
Run Code Online (Sandbox Code Playgroud)

我想要的输出应该是:

 No  = chris
 Yes = hello_world_array
 Yes = another_example_array
 No  = not_this_one
 No  = hello_world
Run Code Online (Sandbox Code Playgroud)

Bor*_*din 6

你需要消极的看后面.即你想要搜索不在前面的字符串的结尾_array.

请注意,您首先需要chomp该行,因为$它将匹配尾随换行符之前和之后.

条件运算符意味着返回一个 - 它不是一个if语句的简写.

use strict;
use warnings;

while (<DATA>) {
  chomp;
  # If the string does not end with '_array' print No, otherwise print Yes
  print /(?<!_array)$/ ? "No  = $_\n" : "Yes = $_\n";
}

__DATA__
chris
hello_world_array
another_example_array
not_this_one
hello_world
Run Code Online (Sandbox Code Playgroud)

产量

No  = chris
Yes = hello_world_array
Yes = another_example_array
No  = not_this_one
No  = hello_world
Run Code Online (Sandbox Code Playgroud)