Python简单正则表达式

2024-10-01 15:43:03 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个模式:

hourPattern = re.compile('\d{2}:\d{2}')

并与编译的模式匹配

^{pr2}$

当我打印hourStart时,它没有给我任何结果。有什么帮助吗?在


Tags: re模式compilepr2hourstarthourpattern
2条回答

匹配方法切换到搜索方法:

>>> hourPattern = re.compile('\d{2}:\d{2}')
>>> hourStart = hourPattern.search('Sat Jan 28 01:15:00 GMT 2012')
>>> hourStart.group()
'01:15'

Match要求找到的值位于字符串的开头。你想要搜查。在

>>> import re
>>>
>>> s = re.compile('\d+')
>>>
>>> s2 = 'a123'
>>>
>>> s.match(s2)
>>> s.search(s2)
<_sre.SRE_Match object at 0x01E29AD8>

相关问题 更多 >

    热门问题