Regex to Match Horizontal White Spaces

前端 未结 3 1475
眼角桃花
眼角桃花 2020-12-11 15:45

I need a regex in Python2 to match only horizontal white spaces not newlines.

\\s matches all whitespaces including newlines.

>&         


        
3条回答
  •  攒了一身酷
    2020-12-11 16:29

    I ended up using [^\S\n] instead of specifying all Unicode white spaces.

    >>> re.sub(r"[^\S\n]", "", u"line 1.\nline 2\n\u00A0\u200A\n", flags=re.UNICODE)
    u'line1.\nline2\n\n'
    
    >>> re.sub(r"[\t ]", "", u"line 1.\nline 2\n\u00A0\u200A\n", flags=re.UNICODE)
    u'line1.\nline2\n\xa0\u200a\n'
    

    It works as expected.

提交回复
热议问题