Python regex match space only

前端 未结 2 405
离开以前
离开以前 2021-01-03 20:22

In python3, how do I match exactly whitespace character and not newline \\n or tab \\t?

I\'ve seen the \\s+[^\\n] answer from Regex match space not \\

2条回答
  •  悲&欢浪女
    2021-01-03 21:18

    If you want to match 1 or more whitespace chars except the newline and a tab use

    r"[^\S\n\t]+"
    

    The [^\S] matches any char that is not a non-whitespace = any char that is whitespace. However, since the character class is a negated one, when you add characters to it they are excluded from matching.

    Python demo:

    import re
    a='rasd\nsa sd'
    print(re.findall(r'[^\S\n\t]+',a))
    # => [' ']
    

    Some more considerations: \s matches [ \t\n\r\f\v] if ASCII flag is used. So, if you plan to only match ASCII, you might as well use [ \r\f\v] to exclude the chars you want. If you need to work with Unicode strings, the solution above is a viable one.

提交回复
热议问题