regex - 如何解决正则表达式中的嵌套量词?

标签 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$text\E/ } @arr_name;

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

关于regex - 如何解决正则表达式中的嵌套量词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21606850/

相关文章:

grep - ack 错过结果(与 grep 相比)

c# - 尽可能长地匹配固定列的行

perl - 只从 perl 中的列表中获取某些值

linux - 在一个文件中查找单词并在另一个文件中获取它们所在的行 : Bash script

mysql - 在 sql 结果中填充空日期的最直接方法是什么(在 mysql 或 perl 端)?

unit-testing - 我如何告诉 Makemaker 并行运行测试?

python - 查找所有希腊文本中带连字符的单词

html - 在 mysql 中删除所有 anchor 标记和其中的任何内容

regex - 在实时验证中使用正则表达式