[英]Regular expression splitting by a specific pattern
I have a string str='\\n1. AA \\n2. BB\\n3.\\n4. CC'
我有一个字符串
str='\\n1. AA \\n2. BB\\n3.\\n4. CC'
str='\\n1. AA \\n2. BB\\n3.\\n4. CC'
str='\\n1. AA \\n2. BB\\n3.\\n4. CC'
. str='\\n1. AA \\n2. BB\\n3.\\n4. CC'
。 I want to split it using the following pattern: a newline character followed by a digit followed by one or more space(s).我想使用以下模式拆分它:换行符后跟数字后跟一个或多个空格。
I am hoping to get the answer ['','AA ', 'BB\\n3.', 'CC']
.我希望得到答案
['','AA ', 'BB\\n3.', 'CC']
。
If I use re.split('\\n[0-9]\\.\\s+',str)
, I get the result:如果我使用
re.split('\\n[0-9]\\.\\s+',str)
,我会得到结果:
['', 'AA ', 'BB', '4. CC']
What am I doing wrong?我究竟做错了什么?
\\s+
at the end matches whitespace including newline characters .末尾的
\\s+
匹配空格,包括换行符。 If you don't want trailing newlines to match change it to [^\\S\\n]+
:如果您不希望尾随换行符匹配,请将其更改为
[^\\S\\n]+
:
>>> re.split('\n[0-9]\.[^\S\n]+',s)
['', 'AA ', 'BB\n3.', 'CC']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.