Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text with multiple separators

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?

like image 685
jezrael Avatar asked Feb 08 '17 09:02

jezrael


People also ask

How do you split with multiple separators?

Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.

Can we use multiple split in Python?

There are multiple ways you can split a string or strings of multiple delimiters in python. The most and easy approach is to use the split() method, however, it is meant to handle simple cases.


2 Answers

You can use re.split to break your strings on either 'A' or 'B':

>>> L1 = [re.split(r'[AB] *', inputtext)[-1] for inputtext in L]
>>> L1
['aaa', 'ffff', 'eeee', 'rrr']
like image 87
wildwilhelm Avatar answered Oct 13 '22 17:10

wildwilhelm


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]
like image 31
tripleee Avatar answered Oct 13 '22 17:10

tripleee