Re: need some regular expression help

2006-10-08 Thread Theerasak Photha
On 10/8/06, Roy Smith <[EMAIL PROTECTED]> wrote: > "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > Certainly true, and it always gives me a hard time because I don't know > > to which extend a regular expression nowadays might do the job because > > of these extensions. It was so much easier back

Re: need some regular expression help

2006-10-08 Thread Roy Smith
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > Certainly true, and it always gives me a hard time because I don't know > to which extend a regular expression nowadays might do the job because > of these extensions. It was so much easier back in the old times What old times? I've been working

Re: need some regular expression help

2006-10-08 Thread bearophileHUGS
Fredrik Lundh wrote: > it's slightly faster, but both your alternatives are about 10x slower > than a straightforward: > def balanced(txt): > return txt.count("(") == txt.count(")") I know, but if you read my post again you see that I have shown those solutions to mark ")))(((" as bad expres

Re: need some regular expression help

2006-10-08 Thread Diez B. Roggisch
Mirco Wahab schrieb: > Thus spoke Diez B. Roggisch (on 2006-10-08 10:49): >> Certainly true, and it always gives me a hard time because I don't know >> to which extend a regular expression nowadays might do the job because >> of these extensions. It was so much easier back in the old times > >

Re: need some regular expression help

2006-10-08 Thread Mirco Wahab
Thus spoke Diez B. Roggisch (on 2006-10-08 10:49): > Certainly true, and it always gives me a hard time because I don't know > to which extend a regular expression nowadays might do the job because > of these extensions. It was so much easier back in the old times Right, in perl, this would be

Re: need some regular expression help

2006-10-08 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > The dict solution looks better, but this may be faster: it's slightly faster, but both your alternatives are about 10x slower than a straightforward: def balanced(txt): return txt.count("(") == txt.count(")") -- http://mail.python.org/mailman/listinfo/python

Re: need some regular expression help

2006-10-08 Thread bearophileHUGS
Tim Chase: > It still doesn't solve the aforementioned problem > of things like ')))(((' which is balanced, but psychotic. :) This may solve the problem: def balanced(txt): d = {'(':1, ')':-1} tot = 0 for c in txt: tot += d.get(c, 0) if tot < 0: return Fal

Re: need some regular expression help

2006-10-08 Thread Theerasak Photha
On 8 Oct 2006 01:49:50 -0700, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > Even if it has - I'm not sure if it really does you good, for several > reasons: > > - regexes - even enhanced ones - don't build trees. But that is what > you ultimately want >from an expression like sin(log(x)) > >

Re: need some regular expression help

2006-10-08 Thread Diez B. Roggisch
hanumizzle wrote: > On 7 Oct 2006 15:00:29 -0700, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > > > > Chris wrote: > > > I need a pattern that matches a string that has the same number of '(' > > > as ')': > > > findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = [ > > > '((2x+2)si

Re: need some regular expression help

2006-10-07 Thread Tim Chase
> Why does it need to be a regex? There is a very simple and well-known > algorithm which does what you want. > > Start with i=0. Walk the string one character at a time, incrementing i > each time you see a '(', and decrementing it each time you see a ')'. At > the end of the string, the co

Re: need some regular expression help

2006-10-07 Thread Roy Smith
In article <[EMAIL PROTECTED]>, "Chris" <[EMAIL PROTECTED]> wrote: > I need a pattern that matches a string that has the same number of '(' > as ')': > findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = [ > '((2x+2)sin(x))', '(log(2)/log(5))' ] > Can anybody help me out? > > Tha

Re: need some regular expression help

2006-10-07 Thread hanumizzle
On 7 Oct 2006 15:00:29 -0700, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > > Chris wrote: > > I need a pattern that matches a string that has the same number of '(' > > as ')': > > findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = [ > > '((2x+2)sin(x))', '(log(2)/log(5))' ] > > C

Re: need some regular expression help

2006-10-07 Thread John Machin
Chris wrote: > I need a pattern that matches a string that has the same number of '(' > as ')': > findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = [ > '((2x+2)sin(x))', '(log(2)/log(5))' ] > Can anybody help me out? > No, there is so such pattern. You will have to code up a func

Re: need some regular expression help

2006-10-07 Thread Diez B. Roggisch
Chris wrote: > I need a pattern that matches a string that has the same number of '(' > as ')': > findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = [ > '((2x+2)sin(x))', '(log(2)/log(5))' ] > Can anybody help me out? This is not possible with regular expressions - they can't "re

need some regular expression help

2006-10-07 Thread Chris
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = [ '((2x+2)sin(x))', '(log(2)/log(5))' ] Can anybody help me out? Thanks for any help! -- http://mail.python.org/mailman/listinfo/python-list