简体   繁体   English

python 如果存在新行则拆分字符串

[英]python split string if new line exists

i have simple string which already decoded to utf-8 below is the result string assigned to s variable我有一个简单的字符串,它已经解码为 utf-8 下面是分配给 s 变量的结果字符串

s = "many simple\n% door needed \n1, but color\n2"

i need to split this string using \n{only number 0 to 9} regex我需要使用 \n{only number 0 to 9} regex 拆分这个字符串

what i already tried is我已经尝试过的是

print(s.split(r"\n+[0-9]")) # this is not working.

in above string only \n1 and \n2 are split should happen but not \n%在上面的字符串中,只有\n1\n2应该发生拆分,但不会发生\n%

so what is correct regex format to spit the string那么什么是正确的正则表达式格式来吐出字符串

import re
s = "many simple\n% door needed \n1, but color\n2"
regex = r"\n[0-9]+"
re.split(regex, s)

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

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