Reputation: 3693
I know how to delete or replace a substring. But I want it only under the condition that the substring occurs non-repetive. I have made a workaround which looks very ugly and I want to know if there is a better solution.
Assuming the following string:
test = "Hello\n,I am here now.\n\nSo what's the problem?"
The first "\n"
should be deletet, however the second "\n\n"
should not.
To do so I I replace the "\n\n"
with something that would never be in a normal conversational string, like:
x = test.replace('\n\n','42#*#')
x = x.replace('\n','')
x = x.replace('42#*#','\n\n')
Which works, but I want to know if there would be a nicer solution to this problem?
edit: I tried the solution from Split a string on a certain character only if it doesn't follow directly after another particular character
However, when using the following regular expression:
re.split('(?<!,)\n', test)
I will get the following result:
['Hello', ',I am here now.', '', "So what's the problem?"]
So both \n
and \n\n
were deleted, how can I avoid this?
Upvotes: 1
Views: 182
Reputation: 16424
You can combine lookahead and lookback assertion:
re.sub(r'\n(?!\n)(?<!\n\n)', '', test)
Upvotes: 5