in reply to Re: Regex in string to be used for matching.
in thread Regex in string to be used for matching.

$regex contains a match operator (Perl source code), not a regex pattern.

Its just a string

my $regex = qr!(?i)\btest\b!;

Replies are listed 'Best First'.
Re^3: Regex in string to be used for matching.
by ikegami (Patriarch) on Nov 09, 2009 at 22:25 UTC

    The match operator will happily interpolate a pattern stored as a string. Compiled patterns have only existed since 5.6. qr// is not necessary.

    In fact, using qr// makes no sense here.

    If there's no reason for the pattern to be a string, you might as well just do

    $string = 'My Test Data'; print $string =~ m/\btest\b/i ? "Match\n" : "No match\n";

    But I bet there's a reason it's a string here. If so, you'd do

    $string = 'My Test Data'; $regex = '(?i)\btest\b'; print $string =~ $regex ? "Match\n" : "No match\n";

    If you were planning on using $regex repeatedly, then qr// could become useful.