in reply to Regex stored in a scalar
I wanted to do something similar to this recently, but with the left and right-hand patterns stored in a database. The problem I ran into, however, was if I tried to use capture variables, such as the following (contrived) example:
perl -Mstrict -Mwarnings -le ' my $c = q{asdfghjk}; my @regex = ( { lh => q{(gh)}, rh => q{__$1__}, }, { lh => q{(h_)}, rh => q{_h!$1!}, }, ); print q{Original: }, $c; foreach my $i ( 0 .. $#regex ) { $c =~ s/$regex[$i]{lh}/$regex[$i]{rh}/gmsx; } print q{Final: }, $c; '
Output, expected:
Original: asdfghjk Final: asdf__g_h!h_!__jk
Output, actual:
Original: asdfghjk Final: asdf__$1__jk
If the value for the rh key is changed to use the qq operator, then "uninitialized value $1 in concatenation" messages appears, and the final string becomes 'asdf____jk'.
Any suggestions?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex stored in a scalar
by Athanasius (Archbishop) on Aug 22, 2015 at 07:41 UTC | |
by AnomalousMonk (Archbishop) on Aug 22, 2015 at 13:55 UTC | |
|
Re^2: Regex stored in a scalar ( s///eeval )
by Anonymous Monk on Aug 22, 2015 at 09:22 UTC | |
by Athanasius (Archbishop) on Aug 23, 2015 at 03:52 UTC |