Reputation: 581
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.
Upvotes: 1
Views: 1071
Reputation: 627507
You can use
re.split(r'\n?^SPLIT$\n?', text, flags=re.M)
re.split(r'(?:^|\n)SPLIT(?:$|\n)', text)
See the Python demo.
The \n?^SPLIT$\n?
regex used with re.M
flag matches an optional newline char, then makes sure the index is at the start of a line (^
) and then matches and consumes SPLIT
and then checks if there is end of a line position right after SPLIT
and then matches an optional newline char.
The (?:^|\n)SPLIT(?:$|\n)
regex just matches either start of string or a newline, SPLIT
and then either end of string or a newline char. Note the use of non-capturing groups so as to avoid having newlines or extra empty strings as part of the resulting list.
See the regex demo #1 and regex demo #2.
Upvotes: 1