a regexp riddle: re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and c') =? ('a', 'bbb', 'c')

python at bdurham.com python at bdurham.com
Thu Nov 25 15:21:16 CET 2010


> 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 )


['cat', 'dog', 'big fish', 'goat', 'puppy', 'horse']


More information about the Python-list mailing list