Regexp cannot be used to format data, but you could pair up regexps and format data:
%patterns = ( t => [ qr/^t\((\d+);(\d+)\)\(([p|q]\d+);([p|q]\d+)\)$/, 't(%d;%d)(%s;%s)' ], inv => [ qr/^inv\((\d+)\)\(([p|q]\d+)([p|q]\d+)\)$/, 'inv(%d)(%s%s)' ], '+' => [ qr/^\+(\d+)$/, '+%d' ] ); my @parsed; foreach ( 't(1;3)(q15;p13)', 'inv(1)(p13p11)', '+3', ) { my $class; foreach my $class (keys(%patterns)) { my @fields; @fields = /$patterns{$class}[0]/ and push(@parsed, [ $class, \@fields ]); } } foreach (@parsed) { my ($class, $fields) = @$_; printf($patterns{$class}[1], @$fields); print(" is of class $class.\n"); } __END__ t(1;3)(q15;p13) is of class t. inv(1)(p13p11) is of class inv. +3 is of class +.
In reply to Re: Parsing/Deparsing a Formatted String
by ikegami
in thread Parsing/Deparsing a Formatted String
by AdriftOnMemoryBliss
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |