python regex error: unbalanced parenthesis

前端 未结 2 1315
无人共我
无人共我 2020-12-17 01:31

I pretty new to python, so i have a dictionary with some keys in it, and a string. I have to replace the string if a pattern found in the dictionary exists in the string. bo

2条回答
  •  生来不讨喜
    2020-12-17 01:42

    Don't use re for something so simple — just replace:

    somedata = somedata.replace(key, 'newvalue')
    

    That said, if you're constructing a regexp from something, use re.escape to escape special characters:

    somedata=re.sub(re.escape(key), 'newvalue', somedata)
    

提交回复
热议问题