Reputation: 27176
I wanted to cut up a string of email addresses which may be separated by any combination of commas and white-space.
And I thought it would be pretty straight-forward :
sep = re.compile('(\s*,*)+')
print sep.split("""[email protected], [email protected]
[email protected],,[email protected]""")
But it isn't. I can't find a regex that won't leave some empty slots like this :
['[email protected]', '', '[email protected]', '', '[email protected]', '', '[email protected]']
I've tried various combinations, but none seem to work. Is this, in fact, possible, with regex?
Upvotes: 7
Views: 12546
Reputation: 59804
without re
line = 'e@d , f@g, 7@g'
addresses = line.split(',')
addresses = [ address.strip() for address in addresses ]
Upvotes: 3
Reputation: 391818
I like the following...
>>> sep= re.compile( r',*\s*' )
>>> sep.split("""[email protected], [email protected]
[email protected],,[email protected]""")
['[email protected]', '[email protected]', '[email protected]', '[email protected]']
Which also seems to work on your test data.
Upvotes: 2