我有一个来自早期文本处理步骤的HTML格式的文本字符串。它是这样的,你看到任何 <SPACE>
字符已被
序列取代:
...this is some text and some further text...
现在,我想用
字符替换文本中<SPACE>
个序列的部分。规则是:
字符替换任何单个 <SPACE>
序列
字符替换一系列
序列中的第一个 <SPACE>
序列结果字符串应如下所示:
...this is some text and some further text...
有关使用Python的程序化方法的任何想法吗?
答案 0 :(得分:1)
问题可以缩短为替换在
之后不会立即出现的每个
。
要实施此策略,请使用re.sub
,并使用负面的后观,如下所示:
import re
s = '...this is some text \
and some further text...'
print(re.sub(r'(?<! ) ', ' ', s))
# ...this is some text and some further text...