我本来是想匹配时间的,无意中发现包含中文的字符串,总体的匹配无法输出,就是 print m.group(0)和 print m.group(1)会报错,这是为什么呢?
# coding=utf-8 timestr= u"2018 年 3 月 28 日" m = re.match(u"((\d+)年(\d+)月(\d+)日)", timestr) print m.group(0) print m.group(1) print m.group(2) print m.group(3)
1 as4069825 2018-08-06 18:22:57 +08:00 是不是括号包括号的问题 |
![]() | 2 ranleng 2018-08-06 18:23:40 +08:00 空格.... |
![]() | 3 ranleng 2018-08-06 18:25:01 +08:00 手滑直接回复了. 按照你的正则 m 是 None >>> m.group Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'group' 改成 m=re.match(u"((\d+) 年 (\d+) 月 (\d+) 日)", timestr) 就没有问题 |
![]() | 5 frmongo OP 哦,你是加了空格,其实原来的字符串没空格 timestr= u"2018 年 3 月 28 日" |
![]() | 6 frmongo OP 我擦,我发现报错的原因是使用了 VScode, 使用 cmd 调用 cpytohn 就可以了.... |