0

I want to implement the following in python.

Given the input:

Text = 'ABC-363738743DEF-2746824769ABC-48738383DEF-437833892'

I want to split this text based on 'ABC' and 'DEF', but also keep them in the output, so I need this:

['ABC-363738743','DEF-2746824769','ABC-48738383','DEF-437833892']

If I use split() then it deletes ABC and DEF.

Could anyone explain to me how I can get this result?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Anne
  • 1
  • Does this answer your question? [In Python, how do I split a string and keep the separators?](https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-separators) – Adam Jaamour Aug 03 '22 at 13:32

1 Answers1

0

Check Below code:

import re 
Text = 'ABC-363738743DEF-2746824769ABC-48738383DEF-437833892'

[ 'A'+i if i.startswith('B') and (len(i)>1) else 'D'+i  for i  in re.split('A|D', Text) ][1:]

Output:

enter image description here

Abhishek
  • 1,585
  • 2
  • 12
  • 15