Phlip,
> I'm trying to match "any word \w+ followed by a comma, or a final word
> preceded by and."
Here's a non-regex solution that handles multi-word values and multiple
instances of 'and' (as pointed out by Alice). The posted code could be
simplified via list comprehension - I chose the more verbose method to
illustrate the logic.
def to_list( text ):
text = text.replace( ' and ', ',' )
output = list()
for item in text.split( ',' ):
if item:
output.append( item.strip() )
return output
test = 'cat, dog, big fish, goat and puppy and horse'
print to_list( test )
Outputs:
['cat', 'dog', 'big fish', 'goat', 'puppy', 'horse']
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list