Python 3.5.1正则表达式并不像预期的那样匹配

时间:2015-12-22 20:02:21

标签: python regex

我已经使用 Regex101 测试了我的正则表达式,但是当我尝试使用 Python 3.5.1 时,它返回None

这是我的Regex101

这是我的Python代码。不确定我是否遗漏了任何东西。

Python 3.5.1 |Anaconda 2.4.0 (x86_64)| (default, Dec  7 2015, 11:24:55)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> p = re.compile('([01]?[0-9]+:?[0-9]*(?:[AP]M)?)-([01]?[0-9]+:?[0-9]*(?:[AP]M)?)', re.MULTILINE)
>>> m = p.match('NO PARKING (SANITATION BROOM SYMBOL) 7AM-7:30AM EXCEPT SUNDAY')
>>> print(m)
None
>>>

1 个答案:

答案 0 :(得分:6)

问题是match将从字符串的开头搜索。请改用search