Note the use of extract_multiple to first separate out all the delimited (quoted, etc), bracketed, and variable-like fields. Also, note that I set the last parameter to 0 so that it will return all the unmatched substrings. These substrings need to be further processed to split them up.my @ef = (extract_multiple($param_string, [ sub { extract_delimited($_[0]) }, sub { extract_bracketed($_[0]) }, sub { extract_variable($_[0]) }, ], undef,0)); my ($extracted, @fields, $field, @subfields); foreach $extracted (@ef) { if ($extracted ne ",") { if ($extracted =~ /^["'`\[\(\$\@\%]/ ) { push(@fields, $extracted); } else { # If this is not the first field, remove any leading # spaces and a comma. If it was the very first field, # then the first comma is significant. if (@fields) { ($extracted) = ($extracted =~ /\s*,?(.*)/); } @subfields = split(/,/, $extracted); # Split on comma foreach $field (@subfields) { ($field) = ($field =~ /([^\s]+)/); # Keep non-space push(@fields, $field); } } } }
At this point I loop thru the extracted fields, pushing them directly on the final @fields list if they were "balanced" text. If they were the unmatched substrings then we will split them on commas, and ignore any whitespace.
To eliminate an extra null param at the beginning of an unmatched substring, I first eat a leading comma (and optional space), but only if this is not the very first parameter in the whole list (you need that one in case there really was a leading null param).
In reply to Re: Text::Balanced extract_multiple does not return all values
by JohnRuf
in thread Text::Balanced extract_multiple does not return all values
by JohnRuf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |