I am trying to split a string with a word that is on a new line by itself.
For example, I want to split the string into parts whenever I encounter the word "SPLIT". For my use case the word "SPLIT" should only be all by itself on a new line:
I tried str.split("\nSPLIT"), but having trouble making it work for after the word.
Hello there,
SPLIT
how are you?
should return ["Hello there,", "how are you?"]
Hello there, SPLIT how are you?
should return ["Hello there, SPLIT how are you?"]
Hello there,
SPLIT
should return ["Hello there,", ""]
Hello there,
SPLIT how are you?
should return ["Hello there,\nSPLIT how are you?"]
Appreciate the help.
re.match( '^(Hello there,\n)(SPLIT\n)?(how are you?)', mystring, re.MULTILINE)split+ regex rather than iterating over and building the substrings again.re.split(r'\n?^SPLIT$\n?', text, flags=re.M)