def remove_after(string, kwrd):
s = string.split(' ')
new = []
skip = []
for i,v in enumerate(s):
if v != kwrd:
if i not in skip:
new.append(v)
else:
skip.append(i+1)
return ' '.join(new)
print(remove_after("grocery store not liquor shop not restaurant, sell milk", 'not'))
如果要删除到下一个标点或行尾的字符,请尝试以下操作:
结果
基本上,删除所有以“not”开头的字符串,后跟空格,后跟所有可用的非(单词或空格)字符,即标点符号。如果您也想去掉尾随逗号,请尝试以下修改:
尾随的
?
确保行的结尾与实际的点蚀匹配。你知道吗这些表达式适用于极端情况,如
如果你不想用re,你可以用循环。你知道吗
你可以这样做:
你会得到这个:
相关问题 更多 >
编程相关推荐