将捕获组替换为将捕获组传递给函数的返回值

  • 本文关键字:函数 返回值 替换 python regex
  • 更新时间 :
  • 英文 :


我正试图用将特定捕获组传递给函数的返回值来替换该捕获组。以下代码使用Python:

def translateWord(word):
... do some stuff
return word
def translateSentence(sentence):
# ([alpha and ']+) [non-alpha]*
# keep the () part, ignore the rest
p = re.compile(r"([a-zA-Z']+)[^a-zA-Z]*")
# find each match, then translate each word and replace
return p.sub(lambda match: translateWord(match.group(1)), sentence)

此代码将替换整个匹配,而不是捕获组。

不良输出示例:

>>> sentence = This isn't my three-egg omelet.
>>> sentence = translateSentence(sentence)
>>> print(sentence)
Isthayisn'tyayymayeethrayeggyayomeletyay

代码需要输出以下内容:

Isthay isn'tyay ymay eethray-eggyay omeletyay.

translateWord()函数应对字符串输入进行操作。我可以测试看看函数接受了什么样的输入,并在此基础上改变行为,但这违背了目的。如何正确地做到这一点?

无论如何,只要尝试一下:

return p.sub(lambda match: translateWord(match.group(1)), sentence)

看起来你对将什么作为第二个参数传递给re.sub感到困惑:你传递了实际的函数(在本例中是lambda表达式(,不需要试图将其嵌入字符串中。

如果您只想更改一个组,re方法不会直接支持它——相反,您必须用整个匹配重新创建一个字符串,替换您想要自己更改的组。

更简单的方法是将您的"lambda"函数扩展为另一个多行函数,该函数将为您完成该操作。然后,它可以使用接收到的匹配对象上的.regs属性来了解组限制(开始和结束(,并构建替换字符串:


def replace_group(match):
sentence = translateWord(match.group(1))
matched = match.group(0)
new_sentence = matched[:match.regs[1][0]] + sentence + matched[match.regs[1][1]:] 
return new_sentence

最新更新