我的正则表达式需要匹配一个句子中的两个单词,但只需要替换第二个单词。第一个词实际上是字典的键,其中获取第二个词的替代项。在 PERL 中,它看起来像:
$sentence = "Tom's boat is blue";
my %mydict = {}; # some key-pair values for name => color
$sentence =~ s/(\S+)'s boat is (\w+)/$1's boat is actually $mydict{$1}/;
print $sentence;
这怎么能在python中完成?