python中校驗手機號 郵箱 密碼的正規表示式

2021-10-24 08:40:53 字數 1628 閱讀 2067

# 方法一:

email_pattern = r"^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z])$"

ret = re.match(email_pattern, "[email protected]")

if ret:

print(ret.group())

方法二:

email_pattern = r"^[a-za-z0-9_-]+(\.[a-za-z0-9_-]+)@[a-za-z0-9_-]+(\.[a-za-z0-9_-]+)$"

ret = re.match(email_pattern, "[email protected]")

if ret:

print(ret.group())

方法三:

import re

def checkemail(email):

regex_1 = '^(\w+)@sina.com$'

regex_2 = '^(\w+)@sina.com.cn$'

regex_3 = '^(\w+)@163.com$'

regex_4 = '^(\w+)@126.com$'

regex_5 = '^[1-9][0,9][email protected]$'

regex = [regex_1 ,regex_2 ,regex_3, regex_4, regex_5]

for i in  regex:

result = re.findall(i,email)

if result:

print("匹配成功")

return true

else:

print("匹配失敗")

return false

email = '[email protected]'

checkemail(email)

首先,我們要知道我們的手機號碼是什麼開頭的?

移動手機號碼開頭有16個號段:134、135、136、137、138、139、147、150、151、152、157、158、159、182、187、188。

聯通手機號碼開頭有7種號段:130、131、132、155、156、185、186。

電信手機號碼開頭有4個號段:133、153、180、189。

import re

from typing import union

phone_pattern = re.compile("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d$")

def is_phone_number(phone: union[str, int]) -> bool:

phone = str(phone)

if not phone.isdigit():

return false

return bool(phone_pattern.match(phone))

ret = is_phone_number("15313067272")

print(ret)

password_pattern = re.compile(r"^[a-za-z_]\w$")

驗證郵箱和手機號

驗證郵箱和手機號 classname regularutil description date 2012 4 26下午06 21 54 public class regularutil 驗證是否是合法的密保答案 param answer 密保答案 return boolean 密保答案只能為數字 字...

iOS 驗證郵箱手機號格式

做登入介面時,使用者在uitextfield中輸入輸入郵箱賬號後,我們應該在本地驗證格式是否正確,再將引數傳給伺服器驗證。最簡單的就是利用系統的nspredicate 利用正規表示式驗證 bool isvalidateemail nsstring email nspredicate emailtes...

身份證校驗,手機號校驗,數字校驗

1 身份證校驗 身份證號 var idcard idcard val 身份證校驗 var reg 1 9 0 9 1 9 0 9 0 9 xx if reg.test idcard 15 0 9 18 0 9 d if myreg.test mobile val alert2 請輸入有效的手機號碼!...