Python separates an unknown string with spaces and parentheses

If I have a line that could be:

'Hello (Test1 test2) (Hello1 hello2) other_stuff' 

I would like to split it into something like this:

 split1='hello' split2='Test1, test2' split3='Hello1, hello2' split4='other_stuff' 

and then I put it in one variable:

 full_split=[split1, split2, split3, split4] 

And also the unknown line will be, if they continue to add words to the end, it will continue to add the shared variable ( split5 , split6 )

I'm looking for a regex, but I don't like to import modules that are not part of python. If I have to.

0
source share
3 answers

Using regex , str.split and str.join :

 >>> 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'] 
+4
source

The standard library has a re module. You can do something like this:

 >>> s="Hello (Test1 test2) (Hello1 hello2) other_stuff" >>> re.findall(r'\w+|\(\w+\s+\w+\)', s) ['Hello', '(Test1 test2)', '(Hello1 hello2)', 'other_stuff'] 

Actually, it depends a lot on how your input looks (spaces? Other parentheses?), So you may need to adapt it to your case.

+4
source

This works and removes empty lines.

 import re, itertools strs = 'Hello (Test1 test2) (Hello1 hello2) other_stuff' res1 = [y for y in re.split(r'\(([^\(]*)\)', strs) if y <> ' '] print res1 
0
source

Source: https://habr.com/ru/post/1488578/


All Articles