在我的 jupyter notebook 中,我想替换打印功能 带有包装函数。
我可以将 print 语句与 javascript regexp print\\(.*\\)
匹配,但从那里我不确定如何在替换文本字段中再次使用匹配:
def verb_printer(msg, verb):
if verb:
print(msg)
我尝试了类似 verb_printer($1)
的方法来访问匹配项,但没有成功。
我该如何解决这个问题?
最佳答案
您的原始表达式很好,我们只会添加一个捕获组 ()
,这样当我们用 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);
关于javascript - 在 jupyter 笔记本中使用匹配替换查找和替换实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427053/