0

I have a string that looks like:

 original_string = "(black && green) || (red && blue)"

I want to pull out all the words and add them to an array. The result I am looking for would be:

vars = ["black", "green", "red", "blue"]

So I've been experimenting with regular expressions, and I wrote the following to split the string:

vars = re.split('\W+', original_string, flags=re.IGNORECASE)

Which sort of works, but returns the following:

vars = [u'', u'black', u'green', u'red', u'blue', u'']

So I turned the list items int strings:

var_list = []
for var in vars:
    var = str(var)
    var_list.append(var)

Which returns:

['', 'black', 'green', 'red', 'blue', '']

So the array has a count of 6 rather than 4 because of the two ' ' in the array. But I am not sure where the ' ' are coming from or how to get rid of them.

4

1 回答 1

2
>>> import re
>>> original_string = "(black && green) || (red && blue)"
>>> re.findall(r'\w+', original_string)
['black', 'green', 'red', 'blue']
于 2013-06-13T19:55:54.407 回答