Python regex, remove all punctuation except hyphen for unicode string

前端 未结 3 1489
南旧
南旧 2020-12-05 05:14

I have this code for removing all punctuation from a regex string:

import regex as re    
re.sub(ur\"\\p{P}+\", \"\", txt)

How would I chan

3条回答
  •  青春惊慌失措
    2020-12-05 05:24

    Here's how to do it with the re module, in case you have to stick with the standard libraries:

    # works in python 2 and 3
    import re
    import string
    
    remove = string.punctuation
    remove = remove.replace("-", "") # don't remove hyphens
    pattern = r"[{}]".format(remove) # create the pattern
    
    txt = ")*^%{}[]thi's - is - @@#!a !%%!!%- test."
    re.sub(pattern, "", txt) 
    # >>> 'this - is - a - test'
    

    If performance matters, you may want to use str.translate, since it's faster than using a regex. In Python 3, the code is txt.translate({ord(char): None for char in remove}).

提交回复
热议问题