Python regex match space only

前端 未结 2 414
离开以前
离开以前 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:05

    No need for special groups. Just create a regex with a space character. The space character does not have any special meaning, it just means "match a space".

    RE = re.compile(' +')
    

    So for your case

    a='rasd\nsa sd'
    print(re.search(' +', a))
    

    would give

    <_sre.SRE_Match object; span=(7, 8), match=' '>
    

提交回复
热议问题