How to match () pairs

P

Peng Yu

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
 
T

Tad J McClellan

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."
 
U

Uri Guttman

TJM> Then you should type:

TJM> perldoc -q match

TJM> How do I find matching/nesting anything?

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
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top