I asked a little question ( Python, separating the unknown string with spaces and parentheses ), which worked fine until I had to change my way of thinking. I still missed the regex, so I need help with this.
If the user types this:
new test (test1 test2 test3) test "test5 test6"
I would like it to look like the output of a variable like this:
["new", "test", "test1 test2 test3", "test", "test5 test6"]
In other words, if this is one word separated by a space, then split it from the next word, if it is in parentheses, then separate the whole group of words in parentheses and delete them. The same goes for quotation marks.
I am currently using this code that does not meet the above standard (from the answers in the link above):
>>>import re >>>strs = "Hello (Test1 test2) (Hello1 hello2) other_stuff" >>>[", ".join(x.split()) for x in re.split(r'[()]',strs) if x.strip()] >>>['Hello', 'Test1, test2', 'Hello1, hello2', 'other_stuff']
This works well, but there is a problem if you have this:
strs = "Hello Test (Test1 test2) (Hello1 hello2) other_stuff"
It combines Hello and Test as one split instead of two.
It also does not allow the use of parentheses and quotation marks at the same time.