Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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.

Replies are listed 'Best First'.
Re^2: making regex case insensitive
by hdb (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044636]
[Discipulus]: choroba's tk cb client become incerdibly stable after https switch; it's a coincidence?
[Discipulus]: it hits some read timeouts but reconnect without issues
erix googles incerdibly and finds incredibly (translate is cleverder than I am)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-06-19 07:27 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.