python正则表达式不匹配捕获组内的空格

时间:2017-11-19 18:59:24

标签: python regex whitespace capture-group

date_re = re.compile(r'^stuff (\d\d-\d\d\s)')
date_re.search('stuff 10-18 16:51').group(1)

由于\s而不匹配。如果我删除空白符号,该组将发现xx-xx数字模式正常。我在正则表达式中的空间做错了什么?

我的完整代码:

>>> print t
stuff 11-16 12:10, Size 1.64 GiB, ULed by 
>>>date_long_re = re.compile(r'^stuff (\d\d-\d\d\s\d\d\d\d)')
>>>size_re = re.compile(r'Size \d+[.\d*]?')
>>>date_re = re.compile(r'^stuff (\d\d-\d\d\s)')
>>>date_re.search(t).group(1)

AttributeErrorTraceback (most recent call last)
<ipython-input-10-18bd3a48937c> in <module>()
      3 size_re = re.compile(r'Size \d+[.\d*]?')
      4 date_re = re.compile(r'^stuff(\d\d-\d\d\s)')
----> 5 date_re.search(t).group(1)

AttributeError: 'NoneType' object has no attribute 'group'

1 个答案:

答案 0 :(得分:-2)

import unicodedata

t= unicodedata.normalize("NFKD", t)

... unicode字符串中有一个愤怒的\xa0