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

In reply to Re: Applying a RegEx in a variable by George_Sherston
in thread Applying a RegEx in a variable by ChOas

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.