Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: making regex case insensitive

by rjt (Deacon)
on Jul 16, 2013 at 17:13 UTC ( #1044636=note: print w/ replies, xml ) Need Help??


in reply to making regex case insensitive

It seems everyone is quite fond of the /i modifier, not to mention their CPU cycles. In colder climates, I find the following helps keep toes warmer, longer:

#!/usr/bin/env perl use 5.012; use warnings; use Carp; say insensitive('This is a test', qr/this is a test/) ? 'Match!' : 'No + match.'; sub insensitive { my ($cmp, $re) = @_; croak "expecting regexp, got " . ref $re unless 'Regexp' eq ref $r +e; carp "Insensitive use of uninitialized value" unless defined $cmp; my ($len, %seen) = length($cmp); croak "Not without bignum" if $len > 31; while (keys %seen <= 2**$len) { return 1 if $cmp =~ $re; substr($cmp, int rand $len, 1) ^= ' '; undef $seen{$cmp}; } return; }

Unicode support is left as an exercise to the reader.


Comment on Re: making regex case insensitive
Select or Download Code
Replies are listed 'Best First'.
Re^2: making regex case insensitive
by hdb (Prior) on Jul 17, 2013 at 06:33 UTC

    How about doing it more directly? The obvious solution seems to be to compare against /test|Test|tEst|.../. The glob function will help to create such a pattern:

    use strict; use warnings; my $pat = "test"; $pat = join "|", glob join "", map { "{".uc.",".lc."}"} split //, $pat +; print "$pat\n"; for ( qw( Test test tesT tast ) ) { print "$_\n" if /$pat/; }
Re^2: making regex case insensitive
by elTriberium (Friar) on Jul 16, 2013 at 18:32 UTC
    You really prefer this solution over adding a single "i" to the regex?
      You really prefer this solution over adding a single "i" to the regex?

      You really didn't know my post was a joke?

        Okay, I'll admit, I missed it. I was trying to figure out what the advantage was of doing it your way.

        I'll go sit in the corner and read Camel or something as pennance.

        Sorry, missed it ;). I misread the part about keeping toes warm and thought you were seriously suggesting this for faster execution time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044636]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2015-07-28 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls