gpt4 book ai didi

python - 用于删除实体名称的正则表达式

转载 作者:行者123 更新时间:2023-11-28 21:48:49 25 4
gpt4 key购买 nike

给定如下推文:

Brick Brewing Co Limited (BRB) Downgraded by Cormark to Market Perform

Brinker International Inc (EAT) Upgraded by Zacks Investment Research to Hold

我如何编写一个正则表达式来删除 “by Cormark”“by Zacks Investment Research”

我试过这个:

"by ([A-Za-z ]+\w to)"

使用 python 但它需要“to”这个词。我希望正则表达式在捕获单词“to”之前停止。

如果有人能告诉我如何编写捕获驼峰案例的正则表达式,如 “Zacks Investment Research”,那也会很有趣。

最佳答案

您可以使用 positive look-ahead为了排除单词 to:

>>> s1 = "Brick Brewing Co Limited (BRB) Downgraded by Cormark to Market Perform"
>>>
>>> s2 = "Brinker International Inc (EAT) Upgraded by Zacks Investment Research to Hold"
>>>
>>> import re
>>> re.sub(r'by[\w\s]+(?=to)','',s1)
'Brick Brewing Co Limited (BRB) Downgraded to Market Perform'
>>> re.sub(r'by[\w\s]+(?=to)','',s2)
'Brinker International Inc (EAT) Upgraded to Hold'
>>>

请注意,正则表达式 [\w\s]+ 将匹配单词字符和空格的任意组合。如果您只想匹配字母字符和空格,您可以使用 [a-z\s]re.I 标志(忽略大小写) .

关于python - 用于删除实体名称的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34595551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com