使用re模塊,輸入兩個手機號碼,進行驗證:
import re pattern = r'(13[4-9]\d{8})$|(15[01289]\d{8})$' mobile = '13634222222' match =re.match(pattern,mobile) if match == None: print(mobile,'不是有效的中國移動手機號碼') else: print(mobile,'是有效的中國移動手機號碼') mobile = '13144222221' match =re.match(pattern,mobile) if match == None: print(mobile,'不是有效的中國移動手機號碼') else: print(mobile,'是有效的中國移動手機號碼')
結果:
13634222222 是有效的中國移動手機號碼
13144222221 不是有效的中國移動手機號碼