473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

match nested parenthesis

hi
i wish to find an reg exp for matching nested parenthesis of varying
level like
string =
"somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
and be able to evaluate the pair starting from the inner most(the
deepest level) , ie (some)
up till the outer most. What is a good reg exp to do this? or is simple
string manipulations enough?
thanks

Jan 23 '07 #1
3 5168

<s9************@yahoo.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
| hi
| i wish to find an reg exp for matching nested parenthesis of varying
| level like
| string =
| "somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
| and be able to evaluate the pair starting from the inner most(the
| deepest level) , ie (some)
| up till the outer most. What is a good reg exp to do this? or is simple
| string manipulations enough?
| thanks

The presence of indefinitely nested fence markers, like parens,
differentiates a context-free language, generated from a context-free
grammar, from a regular language/grammar. You cannot parse such sentences
with a regex engine or even with an extended regex engine such as Python's.
Use a more general parser.

tjr

Jan 23 '07 #2
On 2007-01-23, s9************@yahoo.com <s9************@yahoo.comwrote:
hi
i wish to find an reg exp for matching nested parenthesis of
varying level like
string =
It is not possible, because the set of strings containing
balanced parenthesis is not regular. Python re's aren't *really*
regular expressions, so you might be able to hack something up
using extensions (I'm not sure if it's been proven that you
can'). I believe regexes are the wrong tool for the job.
"somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
and be able to evaluate the pair starting from the inner
most(the deepest level) , ie (some) up till the outer most.
What is a good reg exp to do this? or is simple string
manipulations enough?
Write a context free grammar and a recognizer for that grammar,
instead. Python developers haven't been able to agree on any such
module to include in the standard distribution yet,
unfortunately. But fortunately (consequently), there are tons of
nice parser and scanner generators available on the net. Try
pyparsing for a start, as it's a recent, maintained package with
excellent support on this group at the moment.

Or just compose your own little function by hand. A stack-based
solution (implemented using a list as a stack) should be easy enough.

--
Neil Cerutti
And now the sequence of events in no particular order. --Dan Rather

--
Posted via a free Usenet account from http://www.teranews.com

Jan 23 '07 #3

s9************@yahoo.com wrote:
hi
i wish to find an reg exp for matching nested parenthesis of varying
level like
string =
"somewords1(words(somewords2)-(some(some)words3)somestuff)somestuff"
and be able to evaluate the pair starting from the inner most(the
deepest level) , ie (some)
up till the outer most. What is a good reg exp to do this? or is simple
string manipulations enough?
thanks
Evaluation using re.sub() and recursion (3 lines of code):

def eval_sub(expr):
r"""Evaluate subexpressions in nested paired delimiters.

For example,
‹ single left-pointing angle quotation &lsaquo;
› single right-pointing angle quotation &rsaquo;
>>eval_sub('‹3 * ‹3 + ‹‹1 + 1› * 2›››')
'21'
>>eval_sub('‹3 * 3 + ‹‹1 + 1› * 2›››') # test
mismatched delimiters
'13\x9b'
>>'\x9b' == '›' # encoding ISO-8859-1
True
>>eval_sub('3 * ‹‹1 + 1› * 2 + 3›') # test absence of
outer delimiters
'3 * 7'
"""
val, n = re.subn("‹([^›‹]+)›", lambda m:
str(eval(m.group(1))), expr)
if n == 0:
return val

return eval_sub(val)
#end

This is just a proof of concept.

Jan 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: chenyu468 | last post by:
Hi, I am using (vc++) of vs.net 2003 for development. In the source code, there are too many right parenthesis in one section. I want to test which left parenthesis for one specified parenthesis....
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
1
by: herve bouhours | last post by:
I know that the limit of nested parentheses for visual C++ 6.0 SP5 is 256. I would like to know if this limit has been increased since visual C++ 6.0 SP5. Thanks in advance. RV
2
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
6
by: EXI-Andrews, Jack | last post by:
the '*' and '+' don't seem to be greedy.. they will consume less in order to match a string: ('aa', 'ab') this is the sort of behaviour i'd expect from '(a+?)(ab)' a+ should greedily...
3
by: Hrvoje Niksic | last post by:
I often have the need to match multiple regexes against a single string, typically a line of input, like this: if (matchobj = re1.match(line)): ... re1 matched; do something with matchobj ......
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
6
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Le Thursday 26 June 2008 15:53:06 oyster, vous avez ιcrit : The construct does not match a whole word but only one char, so means "any char which is not t, a, b, l or e". Anyway the inside...
25
by: joeferns79 | last post by:
I had posed a similar topic some time back but I want some additional information from the input file. The log file is as shown... - App Number: 0 - Response for AE Completion No such...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shζllξpτpο 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.