Python 我正在尝试编写一个正则表达式来捕获特定的单词替换它
我有以下声明Python 我正在尝试编写一个正则表达式来捕获特定的单词替换它,python,regex,Python,Regex,我有以下声明 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
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$')
你很接近。在起点和终点使用边界 例: 输出:
你很接近。在起点和终点使用边界 例: 输出:
关于subr'\bmy\b',代词?为什么使用正则表达式?你可以简单地使用句子。replacemy,这个代词必须是正则表达式吗?您是否可以不使用replace方法:str.replacemy,您需要使用regex进行替换,以了解各种regexdelimeters@Rakesh我试过了,但这只替换了第一个出现的cere.subr'\bmy\b',代词?为什么要使用正则表达式?你可以简单地使用句子。replacemy,这个代词必须是正则表达式吗?您是否可以不使用replace方法:str.replacemy,您需要使用regex进行替换,以了解各种regexdelimeters@Rakesh我试过了,但这只取代了第一次
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.