Python regular expression to remove all square brackets and their contents

后端 未结 3 991
说谎
说谎 2021-01-11 21:54

I am trying to use this regular expression to remove all instances of square brackets (and everything in them) from strings. For example, this works when there is only one p

3条回答
  •  旧时难觅i
    2021-01-11 22:42

    Try:

    import re
    pattern = r'\[[^\]]*\]'
    s = """Issachar is a rawboned[a] donkey lying down among the sheep pens.[b]"""
    t = re.sub(pattern, '', s)
    print t
    

    Output:

    Issachar is a rawboned donkey lying down among the sheep pens.
    

提交回复
热议问题