I have list of strings with separators A and B:
L = ['sgfgfqds A aaa','sderas B ffff','eeee','sdsdfd A rrr']
and need:
L1 = [['aaa'], ['ffff'], ['eeee'], ['rrr']]
I tried using:
L1 = [re.findall(r'(?<=A)(.*)$', inputtext) for inputtext in L]
print (L1)
but, it returns the following:
[[' aaa'], [], [], [' rrr']]
How can I get the desired output?
Alternative suggestion without regex.
[[i] for i in ' '.join(L).split(' ') if i.count(i[0]) == len(i) and len(i) > 1]
Result
[['aaa'], ['ffff'], ['eeee'], ['rrr']]
You can use the fact that split returns a list even if it doesn't find the separator.
L1 = [[x.split(' A ')[-1].split(' B ')[-1]] for x in L]