Regex to Match Horizontal White Spaces

前端 未结 3 1468
眼角桃花
眼角桃花 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.

    0 讨论(0)
  • 2020-12-11 16:35

    If you only want to match actual spaces, try a plain ( )+ (brackets for readability only*). If you want to match spaces and tabs, try [ \t]+ (+ so that you also match a sequence of e.g. 3 space characters.

    Now there are in fact other whitespace characters in unicode, that's true. You are, however, highly unlikely to encounter any of those in written code, and also pretty unlikely to encounter any of the less common whitespace chars in other texts.

    If you want to, you can include \u00A0 (non-breaking space, fairly common in scientific papers and on some websites. This is the HTML  ), en-space \u2002 ( ), em-space \u2003 ( ) or thin space \u2009 ( ).

    You can find a variety of other unicode whitespace characters on Wikipedia, but I highly doubt it's necessary to include them. I'd just stick to space, tab and maybe non-breaking space (i.e. [ \t\u00A0]+).

    What do you intend to match with \h, anyway? It's not a valid "symbol" in regex, as far as I know.

     

    *Stackoverflow doesn't display spaces on the edge of inline code

    0 讨论(0)
  • 2020-12-11 16:36

    As there are fewer vertical white space characters (line terminators) than horizontal ones, it will be shorter to blacklist the first category than to white list the second category. But you still need to list a few more than just \n:

    [^\S\n\v\f\r\u2028\u2029]
    
    0 讨论(0)
提交回复
热议问题