Remove numbers from string square bracket

前端 未结 1 1971
一整个雨季
一整个雨季 2021-01-28 09:05

I have a huge string which contains a lot of numbers in square brackets. For instance:

[1] this is an example ... [123] another example

How can I remove the numb

相关标签:
1条回答
  • 2021-01-28 09:24

    Try using re.sub:

    import re
    
    text = txtFile.read()
    text = str(text)
    text = re.sub(r'\[\d+\]', '', text)
    

    The regex pattern \[\d+\] should match any bracket term which has one or more numbers in it.

    Note that re.sub by default will do a replacement against the entire input string.

    0 讨论(0)
提交回复
热议问题