Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if text has certain format?

How to check if text/string has (number:number-number) format in Python?

An example is (7:10-9)

I think I need to use Regex?

like image 296
user1870840 Avatar asked Dec 13 '25 10:12

user1870840


1 Answers

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.

like image 60
Lev Levitsky Avatar answered Dec 15 '25 04:12

Lev Levitsky