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 09:21:16 EST 2010
- Previous message (by thread): a regexp riddle: re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and c') =? ('a', 'bbb', 'c')
- Next message (by thread): a regexp riddle: re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and c') =? ('a', 'bbb', 'c')
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
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
- Previous message (by thread): a regexp riddle: re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and c') =? ('a', 'bbb', 'c')
- Next message (by thread): a regexp riddle: re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and c') =? ('a', 'bbb', 'c')
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the Python-list
mailing list