Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

making regex case insensitive

by Anonymous Monk
on Jul 16, 2013 at 14:03 UTC ( #1044609=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks

I am doing a regex to ensure that the array element has a pattern which I wish to search , say which is test ; so I did

if ($array_A[$ds] =~ /Test/)

which is working fine , just the thing is I want to make it case insensitive since I can encounter test , Test , TEST etc so how can I look out for all of them with regex

Thanks in advance

Comment on making regex case insensitive
Download Code
Re: making regex case insensitive
by MidLifeXis (Prior) on Jul 16, 2013 at 14:08 UTC

    See perlre, and search for insensitive.

    --MidLifeXis

Re: making regex case insensitive
by scorpio17 (Monsignor) on Jul 16, 2013 at 14:15 UTC

      There is also:

      /(?i:test)/

      This is especially useful if you want part of the pattern case-sensitive and part not. For example, the first letter may be upper or lowercase but the rest must be lowercase.

      /(?i:t)est/
Re: making regex case insensitive
by marinersk (Chaplain) on Jul 16, 2013 at 15:41 UTC
    Both preceding answers are excellent; one tells you how to find the answer, the other shows it to you.

    I would add that if you are manually processing each element of the array, you may be doing it the hard way:

    #!/usr/bin/perl # testgreparray.pl - Check behavior of grep on arrays use strict; my @cmdrsp = ( 'This is a test.', 'A booger a day keeps the doctor at bay.', 'Blimey, laddy, it\'s a booger!' ); if (grep(/booger/, @cmdrsp)) { print "Found it the easy way\n"; } my @searsp = grep(/booger/, @cmdrsp); my $seacnt = @searsp; if ($seacnt) { print "Found it the hard way\n"; }
Re: making regex case insensitive
by rjt (Deacon) on Jul 16, 2013 at 17:13 UTC

    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.

      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?

      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: making regex case insensitive
by mmnormyle (Initiate) on Jul 17, 2013 at 00:33 UTC
    just add /i. if ($array_A$ds =~ /Test/i)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1044609]
Approved by marto
Front-paged by rjt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-12-21 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (108 votes), past polls