Extra blank elements when splitting a string with re.split

I am trying to break a line similar to Python using re.split:

#NAME="Foo" NAME2="foobar" NAME3="BAR BAR" comp = "NAME=\"Foo\" NAME2=\"FOO BAR\" NAME3=\"BAR BAR\"" 

This is what my split function looks like, including regex:

 re.split('(\s\w+\=\".*?\")', comp) 

The result is as follows:

 ['NAME="Foo"', 'NAME2="foobar"', '', 'NAME3="BAR BAR"', ''] 

As long as this is correct, I would like to get rid of all empty elements.

+8
python regex
source share
2 answers

This is what you are looking for:

 In [10]: re.findall(r'\w+=".*?"', comp) Out[10]: ['NAME="Foo"', 'NAME2="FOO BAR"', 'NAME3="BAR BAR"'] 

?

It doesn't look like re.split() is the right tool for the job.

+8
source share

You can also use list comprehension and filter it directly.

 l = [x for x in re.split('(\s\w+\=\".*?\")', comp) if x != ''] 

The result looks as you expect:

 print l ['NAME="Foo"', ' NAME2="FOO BAR"', ' NAME3="BAR BAR"'] 
+2
source share

All Articles