Reputation: 1503
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"]
Upvotes: 2
Views: 459
Reputation: 44279
Since lookarounds don't actually advance the "cursor" of the regex engine in the subject string, you can simply check for two conditions in the same place, by writing two lookarounds after one another:
(?<![ ])(?<!\A)\n
Note that \A
matches a position between characters instead of a character, so a lookahead works just as well:
(?<![ ])(?!\A)\n
The square brackets are not necessary, but I find they aid readability in that they make it easier to spot literal space characters.
Upvotes: 2