Vorrei rispondere this question conautorevole espressione regolare per (s) FORMATO printf stringa
Per ottenere tutte le fantasia formattazione di Perl e accesso con chiave di hash dei dati, è bisogno di una (versione migliore di questo) funzione:
# 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;
}
ma mi vergogno di un approccio rischioso/forza bruta alla cattura della stringa di formato 'coda'.
Quindi: Esiste un'espressione regolare per la stringa FORMAT di cui ci si può fidare?
Sei consapevole che '%%' non è un caso speciale in 'sprintf'? Ad esempio 'sprintf ('% 2%')' produrrebbe la stringa ''%''. –