[Solved] Determine whether the input matches a specified format


Using a regex would work like this:

import re

regex = r'\d-\d{4}-\d{4}-\d'

preg = re.compile(regex)

s1 = '9-9715-0210-0'
s2 = '997-150-210-0'

m1 = preg.match(s1)
m2 = preg.match(s2)

if m1:
    print('String s1 is valid')
else:
    print('String s1 is invalid')

if m2:
    print('String s2 is valid')
else:
    print('String s2 is invalid')   

You can try the code at ideone.com.

The regex you suggested in the comment below your question is just the long version of mine. So that should work, too.

1

solved Determine whether the input matches a specified format