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

After parsing a file and extracting the following line-
my $string = "hello $1";
# print $string = 'hello $1

I would like to apply this string as a regexp replacement. For example-
if ("good morning all" =~ /good morning (\w+)/) {
print $string;
# I would like to be written 'hello world add'
}

Replies are listed 'Best First'.
Re: Extracting regex from string
by AnomalousMonk (Archbishop) on Feb 18, 2017 at 05:56 UTC

    Please see Re: Evaluating $1 construct in literal replacement expression to begin your journey.

    Update: Or am I reading more into your question than you intend? Maybe just:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $string = 'hello $1'; ;; if ('good morning all' =~ /good morning (\w+)/) { my $evaled_string = eval qq{ qq{$string} }; print qq{>$evaled_string<}; } " >hello all<
    See eval. ("After parsing a file and extracting the following line..." Note that using string eval in this way is a potentially dangerous operation.)


    Give a man a fish:  <%-{-{-{-<

Re: Extracting regex from string
by LanX (Saint) on Feb 18, 2017 at 10:28 UTC
    Please edit your post to correct code-tags. Additionally 'hello world add' doesn't make sense.

    Please see How do I post a question effectively?

    The better the question the better the answers!

    I need to guess what you really want:

    my $input = "good morning all"; my $pattern = qr/good morning (\w+)/; my $replace = '"hello $1"'; my $output = $input =~ s/$pattern/$replace/ree; print $output;

    hello all

    Please be sure to have full control of the $replace strings since the eval /ee would lead to security issues.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Extracting regex from string
by LanX (Saint) on Feb 18, 2017 at 12:33 UTC
    as an alternative which avoids the risks of eval try sprintf

    $repl = 'hallo %s'; $string = sprintf $repl,$1,$2,$3,$4,$5,$6,$7,$8,$9;

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Also, the n$ "format parameter index" attribute of the sprintf format specifier can free you from the capture variable lockstep of $1 .. $n:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $template = 'all the %3$s %1$s %2$s'; my $extract = 'all at sea in ships'; ;; if ($extract =~ m{ \A all \s+ (\w+) \s+ (\w+) \s+ in \s+ (\w+) \z }xm +s) { my $string = sprintf $template, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13; print qq{>$string<}; } " >all the ships at sea<
      Both safe and flexible.


      Give a man a fish:  <%-{-{-{-<

Re: Extracting regex from string
by BillKSmith (Monsignor) on Feb 18, 2017 at 19:29 UTC
    In your example, $1 seems to take the value 'world'. Where did that come from? It is not in either of your input strings. When is it interpolated? Please post a complete example with both the actual and the desired output.
    Bill