删除空格分隔的单个字符
我有这样的文本:
the quick brown fox ?? m i c r o s o f t ? ? ? ? ? ? ? jumps over the lazy dog ???? best wishes : John Doe
什么是好的正则表达式(对于python)可以删除单个字符,以便输出如下所示:
the quick brown fox ?? jumps over the lazy dog ???? best wishes John Doe
我尝试了 的一些组合s{1}S{1}s{1}S{1},但它们最终不可避免地删除了比我需要的更多的字母。
回答
非正则表达式版本可能如下所示:
source_string = r"this is a string I created"
modified_string =' '.join([x for x in source_string.split() if len(x)>1])
print(modified_string)