How to match a new line character in Python raw string

前端 未结 4 1445
無奈伤痛
無奈伤痛 2020-12-13 13:08

I got a little confused about Python raw string. I know that if we use raw string, then it will treat \'\\\' as a normal backslash (ex. r\'\\n\' wo

4条回答
  •  佛祖请我去吃肉
    2020-12-13 13:41

    def clean_with_puncutation(text):    
        from string import punctuation
        import re
        punctuation_token={p:'' for p in punctuation}
        punctuation_token['
    ']="" punctuation_token['\n']="" punctuation_token['']='' punctuation_token['']='' #punctuation_token regex = r"(
    )|()|()|[\n\!\@\#\$\%\^\&\*\(\)\[\]\ {\}\;\:\,\.\/\?\|\`\_\\+\\\=\~\-\<\>]" ###Always put new sequence token at front to avoid overlapping results #text = '!@#$%^&*()[]{};:,./<>?\|`~-= _+\
    \n \ ' text_="" matches = re.finditer(regex, text) index=0 for match in matches: #print(match.group()) #print(punctuation_token[match.group()]) #print ("Match at index: %s, %s" % (match.start(), match.end())) text_=text_+ text[index:match.start()] +" " +punctuation_token[match.group()]+ " " index=match.end() return text_

提交回复
热议问题