2

I have a string that I want to split by new line characters, but I want to leave the \n character in if it is at the start of the string. I know that \A matches the start of a string, but I don't know how to negate it. I'm guessing I would use something like:

re.split(r"(expression here) & (?<! )\n", text)

(I'm also leaving \n characters preceded by spaces in)

Can anyone point me in the right direction?

An example:

"
:10 e:1110 h:1111 l:110 o:000 x:001 y:010 z:011
11111110110110000100011001010011"

or

"\n:10 e:1110 h:1111 l:110 o:000 x:001 y:010 z:011\n11111110110110000100011001010011"

should come out as

["\n:10 e:1110 h:1111 l:110 o:000 x:001 y:010 z:011","11111110110110000100011001010011"]
4

1 回答 1

2

由于环视实际上并没有在主题字符串中推进正则表达式引擎的“光标”,因此您可以通过一个接一个地编写两个环视来简单地检查同一位置的两个条件:

(?<![ ])(?<!\A)\n

请注意,它\A匹配字符之间的位置而不是字符,因此前瞻同样有效:

(?<![ ])(?!\A)\n

方括号不是必需的,但我发现它们有助于提高可读性,因为它们更容易发现文字空格字符。

于 2013-05-02T10:54:11.463 回答