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

making regex case insensitive

by Anonymous Monk
on Jul 16, 2013 at 14:03 UTC ( [id://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

Replies are listed 'Best First'.
Re: making regex case insensitive
by MidLifeXis (Monsignor) on Jul 16, 2013 at 14:08 UTC

    See perlre, and search for insensitive.

    --MidLifeXis

Re: making regex case insensitive
by scorpio17 (Canon) 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 (Priest) 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 (Curate) 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.

      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/; }
      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?

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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found