1. 程式人生 > 實用技巧 >克里希納駝奶粉食療可養生?

克里希納駝奶粉食療可養生?

例項一:正則匹配郵箱

# 匹配所有的郵箱
import re str1 = '[email protected] [email protected] [email protected] [email protected] [email protected]' reg_str1 = r'(?:[0-9a-zA-Z_]+.)+@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}'#正則匹配出郵箱 mod = re.compile(reg_str1) items = mod.findall(str1) print(items)
# 字串中是否存在郵箱地址
import
re text="
[email protected]" if re.search(r'[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}',text): print('has Email address') else: print('No Email address!')

只允許英文字母、數字、下劃線、英文句號、以及中劃線組成

^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$

名稱允許漢字、字母、數字,域名只允許英文域名

^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$

 

例項二: 正則匹配IP地址

pattern = r'^((1[0-9][0-9]\.)|(2[0-4][0-9]\.)|(25[0-5]\.)|([1-9][0-9]\.)|([0-9]\.)){3}' \
       '([0-1]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])$'

mod = re.compile(pattern)
res = mod.search('255.25.25.111')

if res:
    print('match success %s' % res)
else:
    print('match fail')