in reply to Store regex modifiers in variables?

You can do it, but not the obvious way.
/(?$modifiers)$pattern/

Replies are listed 'Best First'.
(tye)Re: Store regex modifiers in variables?
by tye (Sage) on Jul 10, 2001 at 00:30 UTC

    Or /(?$modifiers:$pattern)/ where $modifiers can be "i-sm", for example, to turn on "i" and turn off "s" and "m". The first form (without a ":" affects the rest of the regex. The second form only affects the stuff between the parens.

    See "perldoc perlre" for more info. See also qr in "perldoc perlop".

            - tye (but my friends call me "Tye")
Re: Re: Store regex modifiers in variables?
by smferris (Beadle) on May 15, 2002 at 16:06 UTC

    I was looking for a similar issue. My problem wasn't with a match, but a substitution. This node is fairly old, however very relevant to my problem and I'm adding my comments in case someone needs a similar answer.

    <bold>Project details:</bold>
    I'm building a "interactive shell" to make our life easier. (DBA Group) In one shell I plan to tie Oracle, Sybase (DBI) and Sybase Replication together. Replication is also managed via DBI, but I provide a customized syntax for manipulating it. (They don't provide bulk loading functionality for heterogeneous environments. Thats done via other Perl scripts/modules) ;) I wanted to provide the user with a way of manipulating the input "buffer" via regex.

    The only modifiers I allow are i,s,m & g for simplicity. ('e' & 'm' would be more difficult to "grammerize" than I'm willing to spend time on) While i,s & m are fine using the syntax (?$mods), I don't believe the g is. Well, no big deal.. My solution:

    if ($mods=~s/g//g) { $buffer=~s/(?$mods)$expr1/$expr2/g; } else { $buffer=~s/(?$mods)$expr1/$expr2/; }

    Hope this helps someone else, like myself! Great thread, thanks to all!

    Regards,
    SMF 8)