Link – Regex101

While doing some Python regex lessons this site was mentioned in user comments, pick language, enter pattern and string. The site will explain how it works to help you better understand.

# Phone number validate, number starts with 1,8, or 9 then has 10 digits after, USA based number. Example input 12132339142
num = input('Number ')
# passed 4/5, had to add $ at end to pass all 5 scenarios
pattern = r"^(1|8|9)(\d{10})$"
# Original pattern = r"^(1|8|9)([0-9]{10})$"
# alt pattern = r"^[189][0-9]{10}$"
# works too, whats the best tho?
match = re.match(pattern, num)
if match:

# Nice one liner
# print("Valid" if re.match(r'[189]\d{10}$', input()) else 'Invalid')

# This was a SoloLearn challenge modified, had an almost fully working answer on first try 4/5 test cases, added the $ and 5/5 pass.
# My solution is not the sleekest but is easy to read, alt pattern and one liner were found in user comments.

Leave a Comment