Override case-sensitive case in Perl

Is it possible to override the case sensitivity of a previously defined regular expression in Perl? For example, if I had the following:

my $upper = qr/BLAH/x; my $lower = qr/$upper/xi; warn "blah" =~ $lower 

I want the third line to print a positive result.

+6
source share
1 answer

You can add /i to 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 this will not affect qr/(?-i:BLAH)/ .


This is more of a question about code reuse, so I don’t need to create two very similar regular expressions that test both uppercase and lowercase letters.

 my $pat = 'BLAH'; my $re1 = qr/$pat/x; my $re2 = qr/$pat/xi; 
+9
source

Source: https://habr.com/ru/post/982270/


All Articles