Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 739
Hedge Avatar asked Jan 31 '13 17:01

Hedge


People also ask

Does split return empty string?

Using split()When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

What happens when you split an empty string in Java?

Java split string on empty delimiter returns empty string at the beginning - Intellipaat Community.

Does Split always return an array?

If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.


2 Answers

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.

like image 116
NPE Avatar answered Sep 30 '22 06:09

NPE


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"']
like image 23
Carlos Quintanilla Avatar answered Sep 30 '22 04:09

Carlos Quintanilla