我有以下声明,

sentence = " Hi my goal is to achieve highest score in mathematics, but my friend has scored some how more. and now i am myself depressed.


我想将所有“我的”替换为“代词”,如下所示。我不想取代我自己就是想保持自己的状态

预期输出为:

Hi PRONOUN goal is to achieve highest score in mathematics, but PRONOUN friend has scored some how more. and now i am myself depressed.


我已经尝试在python中遵循正则表达式

 regex = re.sub(r'\\bmy$')

最佳答案

你很亲密在开始和结束处使用边界。

例如:

import re

sentence = "Hi my goal is to achieve highest score in mathematics, but my friend has scored some how more. and now i am myself depressed."
print(re.sub(r'\bmy\b', "PRONOUN", sentence))


输出:

Hi PRONOUN goal is to achieve highest score in mathematics, but PRONOUN friend has scored some how more. and now i am myself depressed.

10-08 11:14