This article describes the method of randomly generating mobile phone numbers and regular verification of mobile phone numbers in Python. Share it for your reference, as follows:
in accordance with
According to the latest mobile phone number regular code in October 2017, the regular code is as follows:(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}
Code
# -*- coding: utf-8 -*- import random def create_phone(): # The second digit second = [3, 4, 5, 7, 8][(0, 4)] # The third digit third = { 3: (0, 9), 4: [5, 7, 9][(0, 2)], 5: [i for i in range(10) if i != 4][(0, 8)], 7: [i for i in range(10) if i not in [4, 9]][(0, 7)], 8: (0, 9), }[second] # The last eight digits suffix = (9999999,100000000) # Splice mobile phone number return "1{}{}{}".format(second, third, suffix) # Generate a mobile phone numberphone = create_phone() print(phone)
Running results
13937342780
15835720604
14589505530
...
PS: Here is another online tool with similar functions for your reference:
Online randomly generated personal information data tool:
http://tools./aideddesign/rnd_userinfo
Verification (using regular verification)
# -*- coding: utf-8 -*- import random import re def create_phone(): # The second digit second = [3, 4, 5, 7, 8][(0, 4)] # The third digit third = { 3: (0, 9), 4: [5, 7, 9][(0, 2)], 5: [i for i in range(10) if i != 4][(0, 8)], 7: [i for i in range(10) if i not in [4, 9]][(0, 7)], 8: (0, 9), }[second] # The last eight digits suffix = (9999999,100000000) # Splice mobile phone number return "1{}{}{}".format(second, third, suffix) # Generate a mobile phone numberphone = create_phone() print(phone) # Regularreg = ("(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}") print("Test passed!" if (phone) else "Test failed!")
Verification results
18662182464
Test passed!15896505277
Test passed!14952715286
Test passed!...
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools./regex/javascript
Regular expression online generation tool:
http://tools./regex/create_reg
For more information about Python, please view the special topic of this site: "Summary of Python mathematical operation skills》、《Summary of Python string operation skills》、《Summary of Python encoding operation skills》、《Python data structure and algorithm tutorial》、《Summary of Python function usage tips》、《Python introduction and advanced classic tutorials"and"Summary of Python regular expression usage》
I hope this article will be helpful to everyone's Python programming.