在Jupyter Notebook中使用Match替代Find and Replace Utility

时间:2019-06-03 12:07:28

标签: javascript regex python-3.x jupyter-notebook regex-group

在我的jupyter笔记本中,我想替换打印功能 具有包装功能。

我可以将打印语句与javascript正则表达式print\\(.*\\)匹配,但是从那里我不确定如何在替换文本字段中再次使用匹配项:

def verb_printer(msg, verb):
    if verb:
        print(msg)

我尝试使用类似verb_printer($1)的方法来访问比赛,但没有成功。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

您的原始表达式很好,我们只需添加捕获组(),以便在将其替换为verb_printer($1)时,已经捕获了所需的字符串:

(print\(.*\))

Demo

测试

const regex = /(print\(.*\))/gm;
const str = `def verb_printer(msg, verb):
    if verb:
        print(msg)
`;
const subst = `verb_printer($1)`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log(result);