For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2008 > how to remove blocks between nested brackets









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to remove blocks between nested brackets
Si

2008-01-31, 8:41 am

Beeing new to perl and not wanting to learn using it like assembler, I
need to delete parts of a single line (possibly long) that are enclosed
between nested brackets:

abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw [xyz: 98-76-[ef]; gh;
ijkl] (mnop)

The comments between [] must be discarded to split with ';' delimiter.
The expected result is:
array[0] == "abcd efgh"
array[1] == "qrst"
array[2] == "uvw (mnop)"

Thanks for a magic formula.
John W. Krahn

2008-01-31, 8:41 am

Si wrote:
> Beeing new to perl and not wanting to learn using it like assembler, I
> need to delete parts of a single line (possibly long) that are enclosed
> between nested brackets:
>
> abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw [xyz: 98-76-[ef]; gh;
> ijkl] (mnop)
>
> The comments between [] must be discarded to split with ';' delimiter.
> The expected result is:
> array[0] == "abcd efgh"
> array[1] == "qrst"
> array[2] == "uvw (mnop)"


$ perl -le'
my $line = q{abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw [xyz:
98-76-[ef]; gh; ijkl] (mnop)};

1 while $line =~ s/\[[^][]*]//g;

my @array = split /\s*;\s*/, $line;

print qq["$_"] for @array;
'
"abcd efgh"
"qrst"
"uvw (mnop)"



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Abigail

2008-01-31, 7:21 pm

_
Si (silicium_at_h@rmony-p.ath.cx) wrote on VCCLXVI September MCMXCIII in
<URL:news:47a1c3a1$0$902$ba4acef3@news.orange.fr>:
:) Beeing new to perl and not wanting to learn using it like assembler, I
:) need to delete parts of a single line (possibly long) that are enclosed
:) between nested brackets:
:)
:) abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw [xyz: 98-76-[ef]; gh;
:) ijkl] (mnop)
:)
:) The comments between [] must be discarded to split with ';' delimiter.
:) The expected result is:
:) array[0] == "abcd efgh"
:) array[1] == "qrst"
:) array[2] == "uvw (mnop)"
:)
:) Thanks for a magic formula.

The magic formula is: \s*(\[[^][]*+(?:(?1)[^][]*+)*\])


my $_ = "abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw " .
"[xyz: 98-76-[ef]; gh;ijkl] (mnop)";

s {\s*(\[[^][]*+(?:(?1)[^][]*+)*\])} {}g;

my @array = split /;\s*/;

say qq {array[$_] == "}, $array [$_], qq {"} for 0 .. $#array;

__END__
array[0] == "abcd efgh"
array[1] == "qrst"
array[2] == "uvw (mnop)"



Abigail
--
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'
sopan.shewale@gmail.com

2008-01-31, 7:21 pm

Hi,

People have already answered your question - i just thought of adding
value to the discussion.

Have a look at the articles:

[1]. http://blog.stevenlevithan.com/archives/regex-recursion
[2]. http://perl.plover.com/yak/regex/samples/slide083.html

This articles will help you to understand the solutions posted in this
discussion and give high level understanding about the problem.

Regards,

--sopan shewale



On Jan 31, 8:20 am, Abigail <abig...@abigail.be> wrote:
> _
> Si (silicium_a...@rmony-p.ath.cx) wrote on VCCLXVI September MCMXCIII in
> <URL:news:47a1c3a1$0$902$ba4acef3@news.orange.fr>:
> :) Beeing new to perl and not wanting to learn using it like assembler, I
> :) need to delete parts of a single line (possibly long) that are enclosed
> :) between nested brackets:
> :)
> :) abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw [xyz: 98-76-[ef]; gh;
> :) ijkl] (mnop)
> :)
> :) The comments between [] must be discarded to split with ';' delimiter.
> :) The expected result is:
> :) array[0] == "abcd efgh"
> :) array[1] == "qrst"
> :) array[2] == "uvw (mnop)"
> :)
> :) Thanks for a magic formula.
>
> The magic formula is: \s*(\[[^][]*+(?:(?1)[^][]*+)*\])
>
> my $_ = "abcd efgh [ij: 123-[456]-klm; nop-789]; qrst; uvw " .
> "[xyz: 98-76-[ef]; gh;ijkl] (mnop)";
>
> s {\s*(\[[^][]*+(?:(?1)[^][]*+)*\])} {}g;
>
> my @array = split /;\s*/;
>
> say qq {array[$_] == "}, $array [$_], qq {"} for 0 .. $#array;
>
> __END__
> array[0] == "abcd efgh"
> array[1] == "qrst"
> array[2] == "uvw (mnop)"
>
> Abigail
> --
> perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com