附加字符串以匹配子字符串

Attach string for matching substring

我有以下字符串

test = "if row['adm.w'] is 'Bad' and row['rem'] not empty"

我想为 row[...] 的标记添加 str(...),如何在正则表达式中执行此操作?我想到了这个,但它没有按预期工作:

re.sub(r"'([^row[']*)'", r"str([''])", test)

我希望最终结果是

test = "if str(row['adm.w']) is 'Bad' and str(row['rem']) not empty"

这应该有效:

>>> re.sub(r"(row\[.*?\])", r"str()", test)
"if str(row['adm.w']) is 'Bad' and str(row['rem']) not empty"