http://qs1969.pair.com?node_id=11141080


in reply to Using sprintf in regular expreession.

s/PATTERN/REPLACEMENT/msixpodualngcer:
>> e Evaluate the right side as an expression.

The whole right side of the regex is evaluated.
You can't make some parts of the REPLACEMENT evaluated and some not. So you should wrap "$3-" and "-$2" in doublequotes and join all parts by concatenation, e.g.: $date =~ s/(\d+)-(\d+)-(\d+)/ "$3-" . sprintf("%02d",$1) . "-$2" /ge;

Upd. You need double quotes to interpolate $3 and $2 (single quotes won't).

Replies are listed 'Best First'.
Re^2: Using sprintf in regular expreession.
by BillKSmith (Monsignor) on Feb 03, 2022 at 15:12 UTC
    Great explanation! See jwkrahn (above) for a simpler form of the required expression.
    Bill