码迷,mamicode.com
首页 > 其他好文 > 详细

网络和常用的正则整理

时间:2017-07-18 11:48:35      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:log   网络   match   travel   get   int   name   dscp   blog   

# !/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])(?=.*?[#?!_@$%^&*-]).{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|            rint|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), jingtongyu@126.com)
    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))

 

网络和常用的正则整理

标签:log   网络   match   travel   get   int   name   dscp   blog   

原文地址:http://www.cnblogs.com/jingtyu/p/7199215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!