[Tutor] Pythonic way to extract delimited substrings

2008-04-14 Thread Malcolm Greene
Suggestions on the best way to extract delimited substrings strings from
a larger string?

Background: I have a long multi-line string with expressions delimited
with '(' and ')' markers. I would like to extract these substrings and
process them in a loop.

Because the left and right delimiters are different from each other
*and* multi-char strings, it would appear that the .split() method would
not be an appropriate tool for this work.

I know how to do this task with regular expressions, but I'm always
cautious about using a bazooka when a hammer will suffice.

What strategy would you recommend?

1. Write a simple parsing function using string primitives (find,
slicing)

2. Use regular expressions

3. Use a 3rd party string processing module

Thanks!
Malcolm

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythonic way to extract delimited substrings

2008-04-14 Thread Kent Johnson
Malcolm Greene wrote:
 Suggestions on the best way to extract delimited substrings strings from
 a larger string?
 
 Background: I have a long multi-line string with expressions delimited
 with '(' and ')' markers. I would like to extract these substrings and
 process them in a loop.

 What strategy would you recommend?

If you just want to get the text between ( and ), re.findall() or 
re.finditer() is probably the simplest way to do it. If it is more 
complicated than that (e.g. nesting, escape chars) then I would probably 
look at pyparsing.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythonic way to extract delimited substrings

2008-04-14 Thread Alan Gauld

Malcolm Greene [EMAIL PROTECTED] wrote in

 Background: I have a long multi-line string with expressions 
 delimited
 with '(' and ')' markers. I would like to extract these substrings 
 and
 process them in a loop.

 I know how to do this task with regular expressions, but I'm always
 cautious about using a bazooka when a hammer will suffice.

Sounds like an ideal candidate for regex and findall to me.

If you have to stop a tank a bazooka may be the right tool...

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor