[Tutor] bracket issue

Alan Gauld alan.gauld at yahoo.co.uk
Sat Apr 15 09:05:56 EDT 2017


On 15/04/17 03:17, Palm Tree wrote:

> s="2*3+3(8-4(5+6+9))+2+3+3(4/4)"
> 
> i want to find expression enclosed in brackets.
> 
> i want outputs to be like:
> (8-4(5+6+9))
> (5+6+9)
> (4/4)
> 

You probably could do it with some fancy regex but personally
I'd investigate a proper text parser. There is a module to
support that in the standard library but I can't recall its
name, however you need a bit of background in parsing to use
it. Google(or bing or...) is your friend.

If you really want to do it manually you need to count the
brackets going in and then back out. So as you scan your
sample data the counter will go 0,1,2,1,0,1,0

If you store those counts along with the string index where
they change you can use that to slice your string accordingly.
Its all a bit messy but should work...


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list