Created
April 2, 2012 14:37
-
-
Save cristianrasch/2283905 to your computer and use it in GitHub Desktop.
Python's for/while else loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 15-19 validate US telephone numbers with optional area code | |
# 800-555-1212, 555-1212, and also (800) 555-1212 | |
regex = re.compile(r'(\d{3}-|\(\d{3}\)\s)?\d{3}-\d{4}') | |
for s in ['800-555-1212', '555-1212', '(800) 555-1212']: | |
match = re.match(regex, s) | |
if not match: | |
print 'Error! %s should be a valid telephone number' % s | |
break | |
else: | |
print 'All telephone numbers are valid, yey!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment