Authoritative regex for string ss printf FORMAT

I would like to answer this question with

To get all the features of Perl fancy formatting and for hash data, you need (the best version):

# sprintfx(FORMAT, HASHREF) - like sprintf(FORMAT, LIST) but accepts # "%<key>$<tail>" instead of "%<index>$<tail>" in FORMAT to access the # values of HASHREF according to <key>. Fancy formatting is done by # passing '%<tail>', <corresponding value> to sprintf. sub sprintfx { my ($f, $rh) = @_; $f =~ s/ (%%) # $1: '%%' for '%' | # OR % # start format (\w+) # $2: a key to access the HASHREF \$ # end key/index ( # $3: a valid FORMAT tail # 'everything' upto the type letter [^BDEFGOUXbcdefginosux]* # the type letter ('p' removed; no 'next' pos for storage) [BDEFGOUXbcdefginosux] ) /$1 ? '%' # got '%%', replace with '%' : sprintf( '%' . $3, $rh->{$2}) # else, apply sprintf /xge; return $f; } 

but I am ashamed of the risky / brute force approach to capturing a tail format string.

So: is there a regular expression for the FORMAT string that you can trust?

+6
source share
2 answers

If you're asking how to do this exactly like Perl, then consult what Perl does.

Perl_sv_vcatpvfn is a sprintf parser and evaluator. (Link to implementation 5.14.2.)

+1
source

The valid format is pretty well described in perldoc -f sprintf . Between the '%' character and the format letter, you can:

  (\d+\$)? # format parameter index (though this is probably # incompatible with the dictionary feature) [ +0#-]* # flags (\*?v)? # vector flag \d* # minimum width (\.\d+|\.\*)? # precision or maximum width (ll|[lhqL])? # size 
0
source

Source: https://habr.com/ru/post/913570/


All Articles