如何在正则求解嵌套的量词

  • 本文关键字:嵌套 量词 regex perl grep
  • 更新时间 :
  • 英文 :


被卡在

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$textE/ } @arr_name;

(可以省略E,因为它在结尾。)

我无法再现您的问题,但是最好将quotemeta用于特殊字符:

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 "$countn";

最新更新