Re: split : string containing brackets
- From: "Prototype" <google@xxxxxxxxxxxxxxxxxxxx>
- Date: 9 Feb 2007 01:42:19 -0800
On 8 Feb, 19:01, Mirco Wahab <wahab-m...@xxxxxx> wrote:
Prototype wrote:
Well, in the examples you have given, I would want the results:
'val1 , val2 , max( val3, val4 )'
$part[0]='val1'
$part[1]='val2'
$part[2]='max(val3,val4)'
All I'm looking for is to do a "simple" split parse of a string on
commas, but avoiding splitting where the comma is enclosed in some
brackets.
OK, then you may count over the brackets
within the regex. I s stupidly took some
not-so-good-parseable examples, so it
took longer. The Regex is relatively
simple (just counting), you'll get the idea.
This is my first shot:
use strict;
use warnings;
my $o;
my $reg = qr/
(?{$o=0}) \s*
(
(?: [^,()\s]+
| \((?{ $o++ })
| \)(?{ $o-- })
| (?(?{ $o }),|\s)
)+
)
[,\s]*/xs;
while( <DATA> ) {
chomp;
my @fields = /$reg/g;
print "$_\n", scalar @fields, "\n", (map " $_\n", @fields), "\n"
}
__DATA__
val1(), val2 , max(val3,val4)
(val0,val1) , val2 , max(val3,val4)
max(val3,val4), val1 , val2 , max(val3(val6,val7),val4)
Regards
M.
Thanks to all for you various comments and solutions. I'm going with
this one, as it does exactly what I need, and has taught me some neat
new regexp stuff.
P.
.
- References:
- split : string containing brackets
- From: Prototype
- Re: split : string containing brackets
- From: Mirco Wahab
- Re: split : string containing brackets
- From: Prototype
- Re: split : string containing brackets
- From: Mirco Wahab
- split : string containing brackets
- Prev by Date: Re: Perl in the comics
- Next by Date: Syntax Error?
- Previous by thread: Re: split : string containing brackets
- Next by thread: Re: split : string containing brackets
- Index(es):
Relevant Pages
|