Click here to get back home

how to remove blocks between nested brackets

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
how to remove blocks between nested brackets Si 01-31-2008
Posted by Si on January 31, 2008, 7:55 am
Please log in for more thread options
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.

Posted by John W. Krahn on January 31, 2008, 8:08 am
Please log in for more thread options
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

Posted by Abigail on January 31, 2008, 11:20 am
Please log in for more thread options
_
Si (silicium_at_h@rmony-p.ath.cx) wrote on VCCLXVI September MCMXCIII in
:) 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 {}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";'

Posted by sopan.shewale@gmail.com on January 31, 2008, 6:14 pm
Please log in for more thread options
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



> _
> Si (silicium_a...@rmony-p.ath.cx) wrote on VCCLXVI September MCMXCIII in
> :) 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 {}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";'


Similar ThreadsPosted
regex challange: collapse nested {} blocks November 19, 2004, 12:29 am
regexp: ignoring nested brackets January 2, 2006, 11:03 am
END blocks and fork March 1, 2005, 2:23 pm
Using (?{}) code blocks and $^R September 19, 2007, 7:06 pm
Can not print blocks to files with << September 18, 2004, 10:39 am
perl Write filehandle blocks. April 29, 2007, 4:11 am
Re: Parsing blocks of text in Perl March 6, 2008, 6:00 am
Angle Brackets vs. foreach April 13, 2005, 9:37 am
Why is my substitution doubling up on brackets? March 27, 2006, 6:47 pm
split : string containing brackets February 8, 2007, 9:53 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap