[英]Python regex how to remove string at the end of sentence that starts with - and ends with a comma?
我有像这样的字符串
docs = ["This is blue -Gary,.", "Let's go swim. Sounds good -Mark,",
"How are you? Mark says yes. Let's go - Grant,"]
如果它在最后一句中以及在“-”和“”之间,我想删除该名称,但我不知道该怎么做。
期望的输出
docs = ["This is blue", "Let's go swim. Sounds good",
"How are you? Mark says yes. Let's go"]
我努力了
re.sub(r'_^\b.*?[,]\s+', '', x))
但这无济于事。
您可以使用
\s*-\s*[^\W\d_]+,(?=\.?$)
请参阅正则表达式演示。 详情:
\s*-\s*
- 用零个或多个空格括起来的连字符[^\W\d_]+
- 一个或多个字母,
- 逗号(?=\.?$)
- 一个正向的前瞻,它需要一个可选的点和字符串位置的结尾紧靠当前位置的右侧。查看 Python 演示:
import re
rx = re.compile( r'\s*-\s*[^\W\d_]+,(?=\.?$)' )
docs = ["This is blue -Gary,.", "Let's go swim. Sounds good -Mark,",
"How are you? Mark says yes. Let's go - Grant,"]
print( [rx.sub('', x) for x in docs] )
输出:
['This is blue.', "Let's go swim. Sounds good", "How are you? Mark says yes. Let's go"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.