I also coded an simple parser in the meantime, which seems to do the job for me but without doing strict error-checking like yours. It also only splits the string on the commas, not yet the keys and values. Because only alphanumerical keys are allowed anyway (I didn't mentioned that before, sorry) there is no need to look for escaped '=', etc., i.e. everything which doesn't start with /\s*[\w_]+=/ is taken as a single key-less value.
#!/usr/bin/perl use strict; use warnings; sub cskwparser { my $string = shift; my $esc = 0; my $quote = 0; my @args; my $narg = 0; CHAR: foreach my $char ( split //, $string ) { if ($esc) { $esc = 0; } elsif ( $char eq '"' ) { $quote = !$quote; } elsif ( $char eq '\\' ) { $esc = 1; } elsif ( $char eq ',' and not $quote ) { $narg++; next CHAR; } $args[$narg] .= $char; } return @args; } # Test loop: local $, = '|'; while (<>) { print cskwparser $_; }
In reply to Re^2: Help with regex for complicated key=value string
by mscharrer
in thread Help with regex for complicated key=value string
by mscharrer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |