interstar
interstar

Reputation: 27176

Python regex split a string by one of two delimiters

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

Answers (3)

Mykola Golubyev
Mykola Golubyev

Reputation: 59804

without re

line = 'e@d , f@g, 7@g'

addresses = line.split(',')    
addresses = [ address.strip() for address in addresses ]

Upvotes: 3

S.Lott
S.Lott

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

interstar
interstar

Reputation: 27176

Doh!

It's just this.

sep = re.compile('[\s,]+')

Upvotes: 14

Related Questions