如何在Python中检查文本/字符串是否有(number:number-number)
格式?
(7:10-9)
就是一个例子
我想我需要使用Regex?
发布于 2012-12-03 03:20:36
是的,那将是最简单的。示例:
In [1]: import re
In [2]: re.match('\(\d+:\d+-\d+\)', '(7:10-9)')
Out[2]: <_sre.SRE_Match at 0x24655e0>
In [3]: re.match('\(\d+:\d+-\d+\)', '(7)')
In [4]:
作为函数:
def match(s):
return bool(re.match('\(\d+:\d+-\d+\)', s))
别忘了查看一下the docs。
https://stackoverflow.com/questions/13672784
复制相似问题