简体   繁体   English

附加字符串以匹配 substring

[英]Attach string for matching substring

I have the following string我有以下字符串

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

I want to add str(...) for those tokens which are row[...] , how can I do this in regex?我想为那些是row[...]的标记添加str(...) ,我该如何在正则表达式中做到这一点? I came up with this and it's not working as intended:我想出了这个,但它没有按预期工作:

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

I want the end result to be我希望最终结果是

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

This should work:这应该有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM