Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > want extract to parenthesis

Reply
Thread Tools

want extract to parenthesis

 
 
NamSa
Guest
Posts: n/a
 
      05-01-2009

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

 
Reply With Quote
 
 
 
 
Bigus
Guest
Posts: n/a
 
      05-01-2009
"NamSa" <> wrote in message
news:c36cb0c9-46ca-487d-a049-...

> 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


 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      05-01-2009
In article
<c36cb0c9-46ca-487d-a049->,
NamSa <> wrote:

> 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'

--
Jim Gibson
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      05-01-2009
On Fri, 1 May 2009 09:20:13 -0700 (PDT), NamSa <> wrote:

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

( )



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
regular expression to insert spaces around parenthesis puzzlecracker Java 0 01-25-2006 07:31 PM
Extract matter between matching parenthesis. Pradeep Perl Misc 5 07-09-2005 03:20 PM
")" expected error when there's supppose to be only 1 set of parenthesis .Net Sports ASP .Net 4 06-29-2005 06:33 PM
bracket or parenthesis number in Documents Larry Smith Computer Support 3 02-15-2005 01:19 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57