How to input a regex in string.replace?

前端 未结 7 1926
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:50

I need some help on declaring a regex. My inputs are like the following:

this is a paragraph with<[1> in between and then there are cases ..         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 07:14

    replace method of string objects does not accept regular expressions but only fixed strings (see documentation: http://docs.python.org/2/library/stdtypes.html#str.replace).

    You have to use re module:

    import re
    newline= re.sub("<\/?\[[0-9]+>", "", line)
    

提交回复
热议问题