我有一个要在字符串中查找的模式列表。这些模式很多,并且包含许多我只想按字面匹配的元字符。所以这是元引用的完美应用\Q..\E
。复杂之处在于我需要将模式的变量列表加入到正则表达式中。
use strict;
use warnings;
# sample string to represent my problem
my $string = "{{a|!}} Abra\n{{b|!!}} {{b}} Hocus {{s|?}} Kedabra\n{{b|+?}} {{b|??}} Pocus\n {{s|?}}Alakazam\n";
# sample patterns to look for
my @patterns = qw({{a|!}} {{s|?}} {{s|+?}} {{b|?}});
# since these patterns can be anything, I join the resulting array into a variable-length regex
my $regex = join("|",@patterns);
my @matched = $string =~ /$regex(\s\w+\s)/; # Error in matching regex due to unquoted metacharacters
print join("", @matched); # intended result: Hocus\n Pocus\n
当我尝试将元引用引入连接操作时,它们似乎没有效果。
# quote all patterns so that they match literally, but make sure the alternating metacharacter works as intended
my $qmregex = "\Q".join("\E|\Q", @patterns)."\E";
my @matched = $string =~ /$qmregex(\s\w+\s)/; # The same error
由于某种原因,当元引用包含在我用作正则表达式的字符串中时,它不起作用。对我来说,它们只有在直接添加到正则表达式时才起作用,/\Q$anexpression\E/
但据我所知,这对我来说不是一个选择。我该如何解决这个问题?