I am speak in poor English
Sorry ..
The characters in the innermost parentheses, I want to extract
(( A ) aa) <- extract A
(bb (B)) <- extract B
(A (AB) (BC)) <- extract AB and BC
A single regex could do it.
-sln
output:
'AA'
'B'
'A,B'
'BC'
---------------------------------
## Capture.pl
## (will capture all inner parenth characters stripping off enclosing white space)
use strict;
use warnings;
# Shrunk down regex:
# /\(\s*((?
?!\s+\))[^()])+)\s*\)/
while (<DATA>)
{
if (my @array = $_ =~
/
\( # Find first open parenth '('
\s* # Trim zero or more white space
( # Start Capture group 1
(?: # grouping
(?!\s+\)) # look ahead, cannot be 1 or more white space followed by ')' char
[^()] # all is ok, grab a character that is not '(' nor ')'
)+ # end grouping, do 1 or more times
) # End Capture group 1, (done only once)
\s* # Trim zero or more white space
\) # Find very next closing parenth ')'
/xg)
{
print "'$_'\n" for (@array);
}
}
__DATA__
(( AA ) aa) <- extract A
(bb (B)) <- extract B
(A (A,B ) (BC)) <- extract AB and BC
( )