3

如何检查文本/字符串(number:number-number) 在 Python 中是否具有格式?

一个例子是(7:10-9)

我想我需要使用正则表达式?

4

1 回答 1

5

Yes, that would be the easiest. Example:

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]: 

As a function:

def match(s):
    return bool(re.match('\(\d+:\d+-\d+\)', s))

Don't forget to look through the docs.

于 2012-12-02T19:20:35.890 回答