Trouble splitting strings with consecutive delimiters
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue May 1 11:12:46 EDT 2012
On Tue, 01 May 2012 04:50:48 +0000, deuteros wrote:
> I'm using regular expressions to split a string using multiple
> delimiters. But if two or more of my delimiters occur next to each other
> in the string, it puts an empty string in the resulting list.
As I would expect. After all, there *is* an empty string between two
delimiters.
> For example:
>
> re.split(':|;|px', "width:150px;height:50px;float:right")
>
> Results in
>
> ['width', '150', '', 'height', '50', '', 'float', 'right']
>
> Is there any way to avoid getting '' in my list without adding px; as a
> delimiter?
Probably. But why not do it the easy way?
items = re.split(':|;|px', "width:150px;height:50px;float:right")
items = filter(None, item)
In Python 3, the second line will need to be list(filter(None, item)).
--
Steven
More information about the Python-list
mailing list