卡住
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 ;
但两者都没起作用。任何人请帮我解决这个问题。
发布于 2014-02-06 19:37:29
您需要生成一个与文本匹配的正则表达式模式。具体来说,你想要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
可以省略,因为它在末尾。)
发布于 2014-02-06 15:28:16
我无法重现您的问题,但使用商塔处理特殊字符是很好的做法:
use warnings;
use strict;
my @arr_name = ('dgsdjhg','bar C/C++ foo', 'bbbb', 'C/C++');
my $str_check = quotemeta 'C/C++';
my $count = grep { /$str_check/ } @arr_name;
print "$count\n";
https://stackoverflow.com/questions/21606850
复制相似问题