Delete substring when it occurs once, but not when twice in a row in python

北城余情 提交于 2021-02-08 09:40:15

问题


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?


回答1:


You can combine lookahead and lookback assertion:

re.sub(r'\n(?!\n)(?<!\n\n)', '', test)


来源:https://stackoverflow.com/questions/49077374/delete-substring-when-it-occurs-once-but-not-when-twice-in-a-row-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!