Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

regex for case insensitive

by rohan_532 (Initiate)
on May 31, 2017 at 06:40 UTC ( [id://1191670]=perlquestion: print w/replies, xml ) Need Help??

rohan_532 has asked for the wisdom of the Perl Monks concerning the following question:

i need to find a match from array which contains a word to be matched in another array below is the code i have done so far

my @needles = ("1-2 Steps", "5-7 Steps", "8-10 Steps", "catch"); my @haystacks = ("move 1-2 steps", "move 8-10 steps to hoist", "catch +the ball"); for my $needle (@needles) { my $found; for my $haystack (@haystacks) { if ($haystack =~ /\i\Q$needle\E\i/) { print "<br>"; print "Found [$needle] in [$haystack]\n"; print "</br>"; $found++; } } if (!$found) { print "<br>"; print "Couldn't find [$needle] anywhere!\n"; print "<br>"; } }

this code only matches the exact string

Replies are listed 'Best First'.
Re: regex for case insensitive
by marinersk (Priest) on May 31, 2017 at 06:50 UTC

    You could have caught this yourself if you would have used strictand warnings:

    #!/usr/bin/perl use strict; use warnings; my @needles = ("1-2 Steps", "5-7 Steps", "8-10 Steps", "catch"); my @haystacks = ("move 1-2 steps", "move 8-10 steps to hoist", "catch +the ball"); for my $needle (@needles) { my $found; for my $haystack (@haystacks) { if ($haystack =~ /\i\Q$needle\E\i/) { print "<br>"; print "Found [$needle] in [$haystack]\n"; print "</br>"; $found++; } } if (!$found) { print "<br>"; print "Couldn't find [$needle] anywhere!\n"; print "<br>"; } }

    Results in Perl yelling at you for using \i:

    S:\Steve\Dev\PerlMonks\P-2017-05-31@0243-RegEx-Case-Insensitive>perl r +egex0a.pl Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i <-- HERE 1\-2\ Steps\i/ at regex0a.pl line 12. Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i1\-2\ Steps\i <-- HERE / at regex0a.pl line 12. <br>Couldn't find [1-2 Steps] anywhere! Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i <-- HERE 5\-7\ Steps\i/ at regex0a.pl line 12. Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i5\-7\ Steps\i <-- HERE / at regex0a.pl line 12. <br><br>Couldn't find [5-7 Steps] anywhere! Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i <-- HERE 8\-10\ Steps\i/ at regex0a.pl line 12. Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i8\-10\ Steps\i <-- HERE / at regex0a.pl line 12. <br><br>Couldn't find [8-10 Steps] anywhere! Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\i <-- HERE catch\i/ at regex0a.pl line 12. Unrecognized escape \i passed through in regex; marked by <-- HERE in +m/\icatch\i <-- HERE / at regex0a.pl line 12. <br><br>Couldn't find [catch] anywhere! <br> S:\Steve\Dev\PerlMonks\P-2017-05-31@0243-RegEx-Case-Insensitive>


    Two ways to fix this.


    Two key changes:

    my $needle_regex = quotemeta $needle; if ($haystack =~ /$needle_regex/i) {

    New code:

    my @needles = ("1-2 Steps", "5-7 Steps", "8-10 Steps", "catch"); my @haystacks = ("move 1-2 steps", "move 8-10 steps to hoist", "catch +the ball"); for my $needle (@needles) { my $found; my $needle_regex = quotemeta $needle; for my $haystack (@haystacks) { if ($haystack =~ /$needle_regex/i) { print "<br>"; print "Found [$needle] in [$haystack]\n"; print "</br>"; $found++; } } if (!$found) { print "<br>"; print "Couldn't find [$needle] anywhere!\n"; print "<br>"; } }


    Or, in keeping with the approach you originally took, only one change needed:

    if ($haystack =~ /\Q$needle\E/i) {

    Resulting in:

    my @needles = ("1-2 Steps", "5-7 Steps", "8-10 Steps", "catch"); my @haystacks = ("move 1-2 steps", "move 8-10 steps to hoist", "catch +the ball"); for my $needle (@needles) { my $found; for my $haystack (@haystacks) { if ($haystack =~ /\Q$needle\E/i) { print "<br>"; print "Found [$needle] in [$haystack]\n"; print "</br>"; $found++; } } if (!$found) { print "<br>"; print "Couldn't find [$needle] anywhere!\n"; print "<br>"; } }

Re: regex for case insensitive
by AnomalousMonk (Archbishop) on May 31, 2017 at 07:51 UTC

    If you're looking for in-line control of case insensitivity, use  (?i) to turn it on;  (?-i) turns it off. See  (?imsx-imsx) in Extended Patterns in perlre.

    c:\@Work\Perl\monks>perl -wMstrict -le "my @needles = ('1-2 Steps', '5-7 Steps', '8-10 Steps', 'cAtCh', '*+ +??er',); my @haystacks = ( 'move 1-2 steps', 'move 8-10 steps to hoist', 'catch the ball', 'du +mb *+??ER', ); ;; for my $needle (@needles) { my $found; for my $haystack (@haystacks) { if ($haystack =~ /(?i)\Q$needle\E/) { print qq{Found [$needle] in [$haystack]}; ++$found; } } ;; if (!$found) { print qq{Could not find [$needle] anywhere!}; } } " Found [1-2 Steps] in [move 1-2 steps] Could not find [5-7 Steps] anywhere! Found [8-10 Steps] in [move 8-10 steps to hoist] Found [cAtCh] in [catch the ball] Found [*+??er] in [dumb *+??ER]


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1191670]
Approved by marinersk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found