I can't think of a really elegant way to do it, that is without (A) requoting your $RegEx or (B) ripping it to bits to get at what's inside it. The only one-liner I thought of that comes anywhere close is:
$String =~ /(??{substr($RegEx,1,-2)})/;
But that has the defects that you have to know you'll only ever have one modifier in $RegEx, and that it doesn't do case-insensitive matching. What we need to be able to do is use variables as regex modifiers - but I don't think we can. Or can we?
It's probably simpler to do the requoting suggested above, but if you can't do that for some reason, and you need the flexibility, I propose brute force:
($x,$s,$m) = split(/\//,$RegEx);
sub p {print "Match: ",$&||"none"," Pre: ",$`||"none"," Post: ",$'||"n
+one","\n"}
if ($m eq 'i') {$String =~ /$s/i;p}
if ($m eq 'm') {$String =~ /$s/m;p}
if ($m eq 's') {$String =~ /$s/s;p}
if ($m eq 'x') {$String =~ /$s/x;p}
if ($m eq 'o') {$String =~ /$s/o;p}
if ($m eq 'im') {$String =~ /$s/im;p}
if ($m eq 'is') {$String =~ /$s/is;p}
if ($m eq 'ix') {$String =~ /$s/ix;p}
if ($m eq 'io') {$String =~ /$s/io;p}
if ($m eq 'mis') {$String =~ /$s/mips;p}
if ($m eq 'mix') {$String =~ /$s/mix;p}
if ($m eq 'mio') {$String =~ /$s/mio;p}
if ($m eq 'smix') {$String =~ /$s/smix;p}
if ($m eq 'smio') {$String =~ /$s/smio;p}
if ($m eq 'xsmio') {$String =~ /$s/xsmio;p}
Of course, this doesn't allow for global and continuous global matching, but that way madness lies...
§
George Sherston