@arr= map { "$_)" } split /\)/, $str;
This method strips the ending parenthesee, but then adds it back.
Another way is with the 'global' flag on a regex, which returns all matches.
@arr= ( $str =~ /\([^)]*\)/g )
سؤال
I realize that I did not make the title good enough, but I am not getting any better.
Assume there is a string
$str = "(aa)(bb)(cc)(dd)(ee)";
That is, there are substrings, enclosed in parenthesis, there is no space between the parenthesis groups, i.e. like ()(), but inside the parenthesis, where i wrote aa, bb, cc, etc, there can be spaces. Parenthesis may be nested, but that is not absolutely important. but there are unknown number of parenthesis groups.
Now I want to split the string in an array of strings, each element having a (balanced) parenthesis enclosed element . that is,
# @arr contains now ("(aa)", "(bb)", "(cc)" .. etc)
of course i can implement a counter based method, but wont perl, being perl, have some built in methods for this? I dont quite know how this particular operation called, so i dont know what to look for, string splitting is too general, no?
edit: splitting parentheses delimmited string in perl <--- searching this is not returning me anything useful, i guess this is due to the fact it is not really DELIMITED, enclosed?
المحلول
@arr= map { "$_)" } split /\)/, $str;
This method strips the ending parenthesee, but then adds it back.
Another way is with the 'global' flag on a regex, which returns all matches.
@arr= ( $str =~ /\([^)]*\)/g )
نصائح أخرى
There are several suggestions.
For example the first:
use strict;
my $str = "(aa)(bb)(cc)(dd)(ee)";
my @arr;
while ($str =~ /(\(.*?\))/ig) {
push @arr, $1;
};
If we ignore nesting, what you want to do is split between )
and (
.
my @arr = split /(?<=\()(?=\()/, $str;
Instead of splitting, you could also extract the parts.
my @arr = $str =~ /( \( [^()]* \) )/xg;
Matching nested parens is just a matter of applying this regex pattern recursively.
my @arr = $str =~ /\G ( \( (?: [^()]++ | (?1) )* \) )/xg;