【发布时间】:2020-10-06 03:46:24
【问题描述】:
我正在处理具有多个音节划分的文本。
一个典型的字符串是这样的
"this good pe-
riod has"
我试过了:
my_string.replace('-'+"\r","")
但是,它不起作用。
我想得到
"this good period has"
【问题讨论】:
-
我的回答对你有用吗?
标签: python-3.x regex nlp
我正在处理具有多个音节划分的文本。
一个典型的字符串是这样的
"this good pe-
riod has"
我试过了:
my_string.replace('-'+"\r","")
但是,它不起作用。
我想得到
"this good period has"
【问题讨论】:
标签: python-3.x regex nlp
这取决于你的字符串如何结束,你也可以使用 my_string.replace('-\r\n', '') 或使用 re.sub 和 -(?:\r?\n|\r) 的可选回车
如果之前和之后必须有一个单词字符,而不是删除行尾的所有连字符,您可以使用环视:
(?<=\w)-\r?\n(?=\w)
例如
import re
regex = r"(?<=\w)-\r?\n(?=\w)"
my_string = """this good pe-
riod has"""
print (re.sub(regex, "", my_string))
输出
this good period has
【讨论】:
It depends how your string ends 开头的答案,比如ideone.com/Qkmu32
匹配-后,应该匹配换行符\n:
my_string = """this good pe-
riod has"""
print(my_string.replace("-\n",""))
# this good period has
【讨论】:
你试过了吗?
import re
text = """this good pe-
riod has"""
print(re.sub(r"-\s+", '', text))
# this good period has
【讨论】: