Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to match () pairs

0 views
Skip to first unread message

Peng Yu

unread,
Jul 25, 2008, 8:05:09 PM7/25/08
to
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

Tad J McClellan

unread,
Jul 25, 2008, 8:52:39 PM7/25/08
to
Peng Yu <Peng...@gmail.com> 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/"

Uri Guttman

unread,
Jul 26, 2008, 2:16:28 AM7/26/08
to
>>>>> "TJM" == Tad J McClellan <ta...@seesig.invalid> writes:

TJM> Peng Yu <Peng...@gmail.com> 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 ------ u...@stemsystems.com -------- 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 ---------

0 new messages