简体   繁体   English

Python 使用新行上的字符串拆分字符串

[英]Python Split string with a string that is on new line

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".例如,每当我遇到“SPLIT”这个词时,我都想将字符串分成几部分。 For my use case the word "SPLIT" should only be all by itself on a new line:对于我的用例,“SPLIT”这个词应该只在一个新行上单独出现:

I tried str.split("\nSPLIT") , but having trouble making it work for after the word.我尝试str.split("\nSPLIT") ,但无法让它在单词之后起作用。

Hello there,
SPLIT
how are you?

should return ["Hello there,", "how are you?"]应该返回 ["Hello there,", "how are you?"]

Hello there, SPLIT how are you?

should return ["Hello there, SPLIT how are you?"]应该返回 ["Hello there, SPLIT 你好吗?"]

Hello there,
SPLIT

should return ["Hello there,", ""]应该返回 ["Hello there,", ""]

Hello there,
SPLIT how are you?

should return ["Hello there,\nSPLIT how are you?"]应该返回 ["Hello there,\nSPLIT 你好吗?"]

Appreciate the help.感谢帮助。

You can use您可以使用

re.split(r'\n?^SPLIT$\n?', text, flags=re.M)
re.split(r'(?:^|\n)SPLIT(?:$|\n)', text)

See the Python demo .请参阅Python 演示

The \n?^SPLIT$\n? \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.re.M标志一起使用的正则表达式匹配可选的换行符,然后确保索引位于行首( ^ ),然后匹配并使用SPLIT ,然后检查SPLIT之后是否有行尾 position 然后匹配可选的换行符。

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. (?:^|\n)SPLIT(?:$|\n)正则表达式只匹配字符串开头或换行符, SPLIT然后匹配字符串结尾或换行符。 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 .请参阅正则表达式演示 #1正则表达式演示 #2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM