I have a string like that :
'srv1(compA1 compA2) srv2(compA3 compA2) srv3(comp4 comp5)'
I need to split it like this :
['srv1(compA1 compA2)', 'srv2(compA3 compA2)', 'srv3(comp4 comp5)' ]
I tried to split it by space but it splits the inner string of comp1 comp2...
In [9]: import re
In [10]: string = 'srv1(compA1 compA2) srv2(compA3 compA2) srv3(comp4 comp5)'
In [11]: re.split(r'(?<=\)) ', string)
Out[11]: ['srv1(compA1 compA2)', 'srv2(compA3 compA2)', 'srv3(comp4 comp5)']
simply you can Try this way
a='srv1(compA1 compA2) srv2(compA3 compA2) srv3(comp4 comp5)'
print a.replace(' s','###s').split('###')
#output
['srv1(compA1 compA2)', 'srv2(compA3 compA2)', 'srv3(comp4 comp5)']
or you can use regular expression
You could split on ') ' and then add the ')' back in like this:
line = 'srv1(compA1 compA2) srv2(compA3 compA2) srv3(comp4 comp5)'
[e if e.endswith(')') else e + ')' for e in line.split(') ')]
['srv1(compA1 compA2)', 'srv2(compA3 compA2)', 'srv3(comp4 comp5)']
It's a bit ugly but it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.