【发布时间】:2018-02-16 04:13:05
【问题描述】:
我需要 Python2 中的正则表达式来仅匹配水平空格而不是换行符。
\s 匹配所有空格,包括换行符。
>>> re.sub(r"\s", "", "line 1.\nline 2\n")
'line1.line2'
\h根本不起作用。
>>> re.sub(r"\h", "", "line 1.\nline 2\n")
'line 1.\nline 2\n'
[\t ] 有效,但我不确定我是否遗漏了其他可能的空白字符,尤其是在 Unicode 中。如 \u00A0(非中断空格)或 \u200A(头发空格)。以下链接中有更多空白字符:https://www.cs.tut.fi/~jkorpela/chars/spaces.html(死链接)
>>> re.sub(r"[\t ]", "", u"line 1.\nline 2\n\u00A0\u200A\n", flags=re.UNICODE)
u'line1.\nline2\n\xa0\u200a\n'
你有什么建议吗?
【问题讨论】:
标签: regex python-2.7 unicode python-unicode