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>"; } }


In reply to Re: regex for case insensitive by marinersk
in thread regex for case insensitive by rohan_532

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.