匹配子字符串的附加字符串。

3

我有以下字符串

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

我希望为那些是 row[...] 的标记添加 str(...),我该如何在正则表达式中实现?我想到了以下内容,但它并不像预期的那样工作:
re.sub(r"'([^row[']*)'", r"str(['\1'])", test)

我希望最终结果是

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

3

这应该可以正常工作:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接