in reply to Parsing/Deparsing a Formatted String

If you have formal specifications for all the formats then you can parse the formats to generate matching regex's and printf format strings.

Sketch code would look something like:

my $format = ...; my $printfStr; my $regexStr; my $paramCount; while ($format) { my ($chunk) = $format =~ s/^(.*?|\[.*?])(?:\[|$)//g; if ($chunk =~ /^\[int]/) {# match an int $printfStr .= "%d"; $regexStr .= "(\\d+)"; ++$paramCount; } elsif ... else {# match the text $printfStr .= $chunk; $regexStr .= "\Q$chunk\E"; } }

Note that you may need to handle variable whitespace and case indesnsitivity and maybe even nesting of format string elements.


Perl is Huffman encoded by design.