Override case sensitive regex in Perl

那年仲夏 提交于 2019-12-01 04:43:30

You can add the /i to the regexp as follows:

use re qw( is_regexp regexp_pattern );

sub make_re_case_insensitive {
   my ($re) = @_;

   return "(?i:$re)" if !is_regexp($re);

   my ($pat, $mods) = regexp_pattern($re);
   if ($mods !~ /i/) {
      $re = eval('qr/$pat/'.$mods.'i')
         or die($@);
   }

   return $re;
}

But that won't affect qr/(?-i:BLAH)/.


This is more of a code reuse question, so I don't have to make two very similar regex that test either uppercase or lowercase.

my $pat = 'BLAH';
my $re1 = qr/$pat/x;
my $re2 = qr/$pat/xi;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!