Hedge
Hedge

Reputation: 16768

Additional empty elements when splitting a string with re.split

I'm trying to split a string looking like this in Python using re.split:

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

This is how my split-function including regex looks like:

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

The result looks like this:

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

While this is correct I'd like to get rid of all empty elements.

Upvotes: 8

Views: 2786

Answers (2)

Carlos Quintanilla
Carlos Quintanilla

Reputation: 13313

You can also use a list comprehension and filter it directly

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

The result looks like what you expect:

print l
['NAME="Foo"', ' NAME2="FOO BAR"', ' NAME3="BAR BAR"']

Upvotes: 2

NPE
NPE

Reputation: 500673

Is this what you're looking for:

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

?

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

Upvotes: 8

Related Questions