diegosse diegosse - 13 days ago 6
Python Question

Using regex to check corrections of input

I'm trying to use regex to checking it's the same with input chars.

To be shorter, i'm writing char to the console and i need to check it with pattern. I must write for ex. 123-45-67-890 to go to the next operation. Any ideas?

Answer

This should works for pattern you need:

import re
def check (s) : return False if re.match('^\d{3}-\d{2}-\d{2}-\d{3}$', s )==None else True

>>> check ('123-4567-890')
False
>>> check ('123-45-67-890')
True
Comments