Python split a string using regex

I would like to split a string by ':' and ' ' characters. However, i would like to ignore two spaces ' ' and two colons '::'. for e.g.

text = "s:11011 i:11010 ::110011  :110010 d:11000"

should split into

[s,11011,i,11010,:,110011, ,110010,d,11000]

after following the Regular Expressions HOWTO on the python website, i managed to comeup with the following

regx= re.compile('([\s:]|[^\s\s]|[^::])')
regx.split(text)

However this does not work as intended as it splits on the : and spaces, but it still includes the ':' and ' ' in the split.

[s,:,11011, ,i,:,11010, ,:,:,110011, , :,110010, ,d,:,11000]

How can I fix this?

EDIT: In case of a double space, i only want one space to appear

Answers


Note this assumes that your data has format like X:101010:

>>> re.findall(r'(.+?):(.+?)\b ?',text)
[('s', '11011'), ('i', '11010'), (':', '110011'), (' ', '110010'), ('d', '11000')]

Then chain them up:

>>> list(itertools.chain(*_))
['s', '11011', 'i', '11010', ':', '110011', ' ', '110010', 'd', '11000']

Need Your Help

jQuery - possible plugin or method for animating background image when scrolling like Windows 8 start screen?

javascript jquery css background jquery-animate

I noticed in the Windows 8 start screen, that when scrolling the background image kind of drags the opposite way. I was wondering if there is a jQuery animation plugin or method (or CSS if possible...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.