网络和常用的正则整理
Posted jimtong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络和常用的正则整理相关的知识,希望对你有一定的参考价值。
# !/usr/bin/env python # -*- coding:utf-8 -*- ‘‘‘ __auth__ = jingtongyu ‘‘‘ REGEX_STRING = { ‘IP‘: r‘^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}‘ ‘(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$‘, ‘MASK‘: r‘^([0-9]|[12][0-9]|3[0-2])$‘, ‘LOCAL_PREF‘: r‘^[1-9]\d*$‘, ‘INT‘: r‘^\d+$‘, ‘PORT‘: r‘^(([1-6]?[0-5]?[0-5]?[0-3]?[0-5]?)|‘ ‘([1-9]?[0-9]?[0-9]?[0-9]?)|([1-9]?[0-9]?[0-9]?)|([1-9]?[0-9]?)|([0-9]?))$‘, ‘DSCP‘: r‘^(([1-6][1-4]?)|([1-5][0-9]?)|([1-9]?))$‘, ‘PASSWORD‘: r‘^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#[email protected]$%^&*-]).{8,}$‘, ‘EMAIL‘: r‘^[-a-z0-9~!$%^&*_=+}{\‘?]+(\.[-a-z0-9~!$%^&*_=+}{\‘?]+)*@([a-z0-9_][-a-z0-9_]*‘ r‘(\.[-a-z0-9_]+)*\.‘ r‘(aero|arpa|biz|com|coop|edu|gov|info|‘ r‘int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])‘ r‘|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$‘, ‘USER_NAME‘: r‘^[a-z0-9_-]{3,15}$‘, ‘PREFIX‘: r‘^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}‘ r‘(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/)([1-2][0-9]|[3][0-2]|[0-9])$‘ } if __name__ == ‘__main__‘: import re M = re.search(REGEX_STRING.get(‘IP‘), ‘192.168.0.2‘) print M and M.group() M = re.search(REGEX_STRING.get(‘MASK‘), ‘12‘) print M and M.group() M = re.search(REGEX_STRING.get(‘LOCAL_PREF‘), ‘23‘) print M and M.group() M = re.search(REGEX_STRING.get(‘INT‘), ‘12‘) print M and M.group() M = re.search(REGEX_STRING.get(‘EMAIL‘), ‘[email protected]‘) print M and M.group() M = re.search(REGEX_STRING.get(‘USER_NAME‘), ‘dss‘) print M and M.group() M = re.search(REGEX_STRING.get(‘PASSWORD‘), ‘testABCWER?2‘) print M and M.group()
>>> t = ‘19:05:30‘ >>> m = re.match(r‘^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])$‘, t) >>> m.groups() (‘19‘, ‘05‘, ‘30‘)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import re print(‘Test: 010-12345‘) m = re.match(r‘^(\d{3})-(\d{3,8})$‘, ‘010-12345‘) print(m.group(1), m.group(2))
以上是关于网络和常用的正则整理的主要内容,如果未能解决你的问题,请参考以下文章