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.
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.
Java split string on empty delimiter returns empty string at the beginning - Intellipaat Community.
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.
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.
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"']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With