want extract to parenthesis

N

NamSa

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
 
B

Bigus

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

my $text = "(A (AB) (BC))";
my @extracted = $text =~ /\(\s*([A-Z]+)\s*\)/g;
print "@extracted";

That works for all the examples you have given...

Bigus
 
J

Jim Gibson

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

Suggestions:

1. Use Text::Balanced
2. Read 'perldoc -q balanced'
 
S

sln

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

( )
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top