在perl模块
Regexp :: Grammars中,请考虑以下标记:
<token: command> <%commands>
这个标记是复杂语法的一部分,解析各种不同的句子.
这个令牌匹配hash%命令中的任何单词,我已经定义如下(当然,在任何函数之外):
our %commands = ( 'Basic_import' => 1,'Wait' => 1,'Reload' => 1,'Log' => 1,);
这适用于匹配“Basic_import”,“Wait”等关键字.但是,我还希望它匹配“basic_import”,“wait”等字样.
如何在不必多次复制和粘贴每个关键字的情况下使此哈希大小写不敏感?因为这是复杂语法的一部分,我想使用Regexp :: Grammars,我宁愿不必为这个特殊异常恢复grep.
解决方法
您可以使用
Hash::Case::Preserve使哈希查找不区分大小写:
use strict; use warnings 'all'; use Data::Dump; use Hash::Case::Preserve; use Regexp::Grammars; tie my %commands,'Hash::Case::Preserve'; %commands = ( 'Basic_import' => 1,); my $grammar = qr{ <command> <token: command> <%commands> }; dd \%/ if 'basic_import' =~ $grammar;
输出:
{ "" => "basic_import","command" => "basic_import" }
请注意,在将任何值插入其中之前必须绑定哈希.