Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl case insensitive switch as variable?

Tags:

regex

perl

In perl, I would like to be able to do this:

$switches = "is";
$regexp = "\\d";
if ($string =~ m/$regexp/$switches) {
 do something...
}

In other words, how can I make "$switches" optional at run-time? (The user can choose from a variety of options for the search)

like image 472
sploiber Avatar asked May 05 '14 14:05

sploiber


1 Answers

if ($string =~ /(?$flags:$pattern)/) {
   ...
}

Note: This won't work if $pattern is a compiled pattern (i.e. produced by qr//) rather than a string since it's the flags passed to qr// that affect a pattern compiled with qr//. You would have to pass the flags to qr// rather than m//.

like image 164
ikegami Avatar answered Sep 22 '22 11:09

ikegami