国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python實(shí)現(xiàn)隨機(jī)生成手機(jī)號(hào)及正則驗(yàn)證手機(jī)號(hào)的方法

2020-01-04 15:21:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)隨機(jī)生成手機(jī)號(hào)及正則驗(yàn)證手機(jī)號(hào)的方法。分享給大家供大家參考,具體如下:

依據(jù)

根據(jù)2017年10月份最新的手機(jī)號(hào)正則進(jìn)行編碼, 正則如下:
(13/d|14[579]|15[^4/D]|17[^49/D]|18/d)/d{8}

代碼

# -*- coding: utf-8 -*-import randomdef create_phone():  # 第二位數(shù)字  second = [3, 4, 5, 7, 8][random.randint(0, 4)]  # 第三位數(shù)字  third = {    3: random.randint(0, 9),    4: [5, 7, 9][random.randint(0, 2)],    5: [i for i in range(10) if i != 4][random.randint(0, 8)],    7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],    8: random.randint(0, 9),  }[second]  # 最后八位數(shù)字  suffix = random.randint(9999999,100000000)  # 拼接手機(jī)號(hào)  return "1{}{}{}".format(second, third, suffix)# 生成手機(jī)號(hào)phone = create_phone()print(phone)

運(yùn)行結(jié)果

13937342780
15835720604
14589505530
...

 

驗(yàn)證 (使用正則驗(yàn)證)

# -*- coding: utf-8 -*-import randomimport redef create_phone():  # 第二位數(shù)字  second = [3, 4, 5, 7, 8][random.randint(0, 4)]  # 第三位數(shù)字  third = {    3: random.randint(0, 9),    4: [5, 7, 9][random.randint(0, 2)],    5: [i for i in range(10) if i != 4][random.randint(0, 8)],    7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],    8: random.randint(0, 9),  }[second]  # 最后八位數(shù)字  suffix = random.randint(9999999,100000000)  # 拼接手機(jī)號(hào)  return "1{}{}{}".format(second, third, suffix)# 生成手機(jī)號(hào)phone = create_phone()print(phone)# 正則reg = re.compile("(13/d|14[579]|15[^4/D]|17[^49/D]|18/d)/d{8}")print("Test passed!" if reg.match(phone) else "Test failed!")

驗(yàn)證結(jié)果

18662182464
Test passed!

15896505277
Test passed!

14952715286
Test passed!

...

 

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大悟县| 邢台县| 九寨沟县| 滁州市| 康保县| 双牌县| 铅山县| 金寨县| 涡阳县| 阿拉善右旗| 洛扎县| 庆城县| 德令哈市| 黎川县| 辽中县| 岳池县| 庐江县| 大新县| 昌平区| 大渡口区| 湖南省| 辽阳县| 韶山市| 漳平市| 招远市| 温州市| 临安市| 乌兰察布市| 华池县| 西充县| 蓝山县| 郯城县| 常熟市| 定远县| 唐山市| 巢湖市| 宝坻区| 平谷区| 尼玛县| 洛南县| 芦山县|