micronx2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

My regex works, just not on hashes? Any clues?
This works: $curline = '...[$variable]...'; # example 1 $curline =~ s/\[\$(.+?)\]/$$1/g; This doesn't: $curline = '...[$hash{'constant'}]...'; # example 2 $curline =~ s/\[\$(.+?)\]/$$1/g;

How should I be going about this?
Thanks,
Steve

Replies are listed 'Best First'.
Re: Regex Variable Substitution
by Prior Nacre V (Hermit) on Aug 06, 2004 at 21:29 UTC

    In the # example 2 line you have 4 single quotes - that's your problem.

    If you had used strict and warnings - Perl would have told you this.

    Change $hash{'constant'} to one of:

    $hash{constant} $hash{q(constant)} $hash{"constant"} $hash{qq(constant)}

    Regards,

    PN5

Re: Regex Variable Substitution
by ysth (Canon) on Aug 06, 2004 at 21:34 UTC
    s/\[\$(.+?)\]/$1/eeg;