卡在
my $count=grep {/$str_check/} @arr_name ;
当
$str_check = 'C/C++'
它抛出 Nested quantifiers in regex; marked by <-- HERE in m/'C/C++ <-- HERE '/ at acr_def_abb_use.pl line 288
我试着改成
my $count=grep {/"$str_check"/} @arr_name ;
和
my $count=grep {/'$str_check'/} @arr_name ;
但是两者都不起作用。任何人都请帮我解决这个问题。
最佳答案
您需要生成与文本匹配的正则表达式模式。具体来说,您需要 C/C\+\+
。
my $text = 'C/C++';
my $pat = quotemeta($text);
my $count = grep { /$pat/ } @arr_name;
或
my $text = 'C/C++';
my $count = grep { /\Q$text\E/ } @arr_name;
(\E
可以省略,因为它在末尾。)
关于regex - 如何解决正则表达式中的嵌套量词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21606850/