【问题标题】:Attach string for matching substring附加字符串以匹配子字符串
【发布时间】:2022-01-03 10:41:06
【问题描述】:

我有以下字符串

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"

【问题讨论】:

    标签: python regex regexp-replace


    【解决方案1】:

    这应该可行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 2018-05-31
      相关资源
      最近更新 更多