Python regex breaks a string into one of two delimiters

I wanted to cut a string of email addresses that can be separated by any combination of commas and spaces.

And I thought it would be pretty straight forward:

sep = re.compile('(\s*,*)+') print sep.split("""a@b.com, c@d.com e@f.com,,g@h.com""") 

But this is not so. I cannot find a regex that does not leave any empty slots like:

 ['a@b.com', '', 'c@d.com', '', 'e@f.com', '', 'g@h.com'] 

I tried various combinations, but nobody works. Is this possible with regex?

+7
python regex
source share
3 answers

Doh!

That is exactly so.

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

without re

 line = 'e@d , f@g, 7@g' addresses = line.split(',') addresses = [ address.strip() for address in addresses ] 
+3
source share

I like the following ...

 >>> sep= re.compile( r',*\s*' ) >>> sep.split("""a@b.com, c@d.com e@f.com,,g@h.com""") ['a@b.com', 'c@d.com', 'e@f.com', 'g@h.com'] 

Which also seems to work with your test data.

+2
source share

All Articles