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

How would I go about doing a printf format of a var within a s/// expression? I'm using a template for a reporting module, but I need to format vars as %.2f within a s/// statement. Any help would be greatly appreciated, oh wise monks.
  • Comment on printf formatting in regular expressions?

Replies are listed 'Best First'.
Re: printf formatting in regular expressions?
by GrandFather (Saint) on Oct 05, 2008 at 20:23 UTC

    Use sprintf and the /e switch on the regex:

    s/(\d+)/sprintf '%04d', $1/e

    Perl reduces RSI - it saves typing
      thanks a lot!