want extract to parenthesis

Discussion in 'Perl Misc' started by NamSa, May 1, 2009.

  1. NamSa

    NamSa Guest

    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
    NamSa, May 1, 2009
    #1
    1. Advertising

  2. NamSa

    Bigus Guest

    "NamSa" <> wrote in message
    news:...

    > 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
    Bigus, May 1, 2009
    #2
    1. Advertising

  3. NamSa

    Jim Gibson Guest

    In article
    <>,
    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
    Jim Gibson, May 1, 2009
    #3
  4. NamSa

    Guest

    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

    ( )
    , May 1, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .Net Sports
    Replies:
    4
    Views:
    2,099
    Eliyahu Goldin
    Jun 29, 2005
  2. puzzlecracker
    Replies:
    0
    Views:
    433
    puzzlecracker
    Jan 25, 2006
  3. Adam Akhtar
    Replies:
    7
    Views:
    91
    daniel hoey
    Mar 31, 2008
  4. Pradeep
    Replies:
    5
    Views:
    174
    Anno Siegel
    Jul 9, 2005
  5. Hongyi Zhao
    Replies:
    2
    Views:
    999
    Hongyi Zhao
    Jan 29, 2009
Loading...

Share This Page