Re: Text::Balanced v Parse::RecDescent
[email protected] (Andrew Savige) Wed, 4 Dec 2002 16:01:21 +1100 (EST)
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
Though a much less interesting problem, in this specific case
I'm pretty sure that nested { , , } are not allowed. Thus, the
following solution seems to work and seems quite fast too.
use re 'eval';
our $quoted = qr/ ' (?: \\. | [^'] )*? ' # Match 'str'
| " (?: \\. | [^"] )*? " # Match "str"
/x;
our $simple = qr/ (?: [^'"{,]+ # Match non-special chars
| \\. # Match escaped anything
| $quoted # Match quoted anything
)+
/xs;
our $element = qr/ (?: [^'"{,]+ # Match non-special chars
| \\. # Match escaped anything
| $quoted # Match quoted anything
| (??{$nested}) # Match {...,...,...}
)+
/xs;
our $nested = qr/ [{] # Match {
(?: $simple , )* # Match list of elements
$simple? # Match last element
[}] # Match }
/x;
$data = <DATA>;
@fields = $data =~ m/\G($element),?/g;
use Data::Dumper 'Dumper';
print Dumper(@fields);
__DATA__
abc, ',def' "\"ab'c,}" xyz , fred IN { 1, "x}y",3 } x, 'z'
/-\
http://www.yahoo.promo.com.au/hint/ - Yahoo! Hint Dropper
- Avoid getting hideous gifts this Christmas with Yahoo! Hint Dropper!