繁体   English   中英

删除撇号后的字符串内容

[英]Remove string contents after apostrophe

我正在尝试从句子中删除撇号标点符号'之后的字符串内容。

例如,给出一句话This is the world's first engine. You'll learn more about this later. This is the world's first engine. You'll learn more about this later.

预期的输出是This is the world first engine. You learn more about this later. This is the world first engine. You learn more about this later.

我可以使用re.sub()删除撇号 ' 。 但是,我无法删除撇号后的内容

re.sub(r'[^\\w]', ' ', value)

我也使用过 python replace() 但是,这不是通用的解决方案

value.replace("'s", "")

任何帮助表示赞赏。 谢谢你!

要删除str中的撇号和尾随字母,您可以使用以下代码。

import re

s = "This is the world's first engine. You'll learn more about this later."
s = re.sub(r'\'\w+', '', s)
print(s)

输出This is the world first engine. You learn more about this later. This is the world first engine. You learn more about this later.

模式\\'\\w+匹配后跟一个或多个单词字符的撇号, re.sub()用于用空字符串 ( '' ) 替换此模式的任何匹配项。

您可以使用re.sub()如下

import re
def clean(value):
    return re.sub(r'\'\w{,2}', '', value)

print(clean("This is the world's first engine. You'll learn more about this later"))

试试这个(我想正则表达式有更简单的解决方案):

s="This is the world's first engine. You'll learn more about this later."

s=' '.join(list(map(lambda x: x[:x.find("'")] if "'" in x else x, s.split(' '))))

>>> print(s)
'This is the world first engine. You learn more about this later.'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM