how to remove blocks between nested brackets

S

Si

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.
 
J

John W. Krahn

Si said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top