如何从python中的字符串中删除子字符串“-”,但保留“-”子字符串?

问题描述 投票:-1回答:1

示例:

string = " a lot of text ... protective equip- ment ... a lot of text - with similar broken words like simple appli- cations ..."

我需要获得相同的文本,但设备变成设备,而应用程序变成应用程序。谢谢

python-3.x string replace nlp str-replace
1个回答
-1
投票

尝试一下:

>>> import re
>>> string = " a lot of text ... protective equip- ment ... a lot of text - with similar broken words like simple appli- cations ..."
>>> re.sub(r"(\w+)- (\w+)", r"\1\2", string)
' a lot of text ... protective equipment ... a lot of text - with similar broken words like simple applications ...'
© www.soinside.com 2019 - 2024. All rights reserved.