我正在尝试编写用于捕获特定单词的正则表达式来替换它

时间:2019-05-03 08:59:13

标签: 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.

我想将所有“ my”替换为“ PRONOUN”,如下所示。我不想替换自己,是我自己要保持现状

预期输出为:

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

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

1 个答案:

答案 0 :(得分:6)

您非常亲密。在开始和结束处使用边界。

例如:

$Constant = "somestring"
$Strings = Get-Content ".\strings.txt"

for($i=0;$i -lt $Strings.count;$i++) {
  $Strings[$i] += $Constant
}

$Strings

输出:

onesomestring
twosomestring
threesomestring