How to match () pairs

Discussion in 'Perl Misc' started by Peng Yu, Jul 26, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I would like to match a pattern such as

    something( something_1( something_2( ...
    (something_n)...)something_else_2)something_else_1)

    , where all the brackets pair up and there could be any number of
    pairs. something_1, something_2, ..., and something_n could contain
    '()' pair as well.

    I'm wondering if there is any way to do this with regex?

    Thanks,
    Peng
    Peng Yu, Jul 26, 2008
    #1
    1. Advertising

  2. Peng Yu <> wrote:


    > I would like to match



    Then you should type:

    perldoc -q match

    How do I find matching/nesting anything?


    > a pattern such as
    >
    > something( something_1( something_2( ...
    > (something_n)...)something_else_2)something_else_1)
    >
    > , where all the brackets pair up and there could be any number of
    > pairs. something_1, something_2, ..., and something_n could contain
    > '()' pair as well.
    >
    > I'm wondering if there is any way to do this with regex?



    "This isn't something that can be done in one regular expression, no
    matter how complicated."


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Jul 26, 2008
    #2
    1. Advertising

  3. Peng Yu

    Uri Guttman Guest

    >>>>> "TJM" == Tad J McClellan <> writes:

    TJM> Peng Yu <> wrote:

    >> I would like to match


    TJM> Then you should type:

    TJM> perldoc -q match

    TJM> How do I find matching/nesting anything?

    >> a pattern such as
    >> something( something_1( something_2( ...
    >> (something_n)...)something_else_2)something_else_1)
    >>
    >> , where all the brackets pair up and there could be any number of
    >> pairs. something_1, something_2, ..., and something_n could contain
    >> '()' pair as well.


    TJM> "This isn't something that can be done in one regular expression, no
    TJM> matter how complicated."

    that could be done with (??{ code }) which is considered experimental
    but 5.10 has (?PARNO) which says it can match nested parens. see perldoc
    perlre and look for PARNO. it is still not for newbies and the faint of
    heart but it is supported. it is still better/simpler to use a module like
    text::balanced to match nested parens.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Jul 26, 2008
    #3
    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. hiwa
    Replies:
    0
    Views:
    630
  2. Victor
    Replies:
    2
    Views:
    629
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    356
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    463
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    155
Loading...

Share This Page