Python正则小实例

def re_test():
    import re
    s = "gsmice@sina.cn : 测试账号 : 7568 : 06286ec389c5536914d63d094f93da7a"
    pattern = re.compile(r"^\s*(?P<name>[^\s:]+)\s*:\s*(?P<nick>[^\s:]+)\s*:\s*(?P<passwd>[^\s:]+)\s*:\s*(?P<token>[^\s:]+)")
    match = pattern.search(s)
    if match:
        print(match.group('name'))
        print(match.group('nick'))
        print(match.group('passwd'))
        print(match.group('token'))
re_test()

def cofig_file_test():
    import re
    s = '"sys_file_path" = "F:/ghost_/src/sys"'
    pattern = re.compile(r'^\s*"(?P<key>.+)"\s*=\s*"(?P<val>.+)"\s*\Z')
    # LINE_PATTERN = re.compile(r'\A\s*"(?P<key>.+?)"\s*=\s*"(?P<val>.+?)"\s*\Z')
    match = pattern.search(s)
    if match:
        print(match.group('key'))
        print(match.group('val'))
    # print(match)
cofig_file_test()

def url_regular():
    import re
    s = "https://ww[w.goog]le.hk"
    # s = "URL_PATTERN = re.compile(r'https?\:\/\/[\_\.\-\?\=\/\&a-z0-9]+', re.IGNORECASE)"
    pattern = re.compile(r'https?:\/\/(?P<url>[_.\[\]a-z]+)')#利用反斜杠对"[","]"进行转换
    match = pattern.search(s)
    if match:
        print(match.group('url'))
url_regular()

def re_test():
    s =  "select sysdate from dual where user= '@user1' and password='@password' or username='@username'"
    pattern = re.compile("'.*?@([a-z\d]+)'.*?'@([a-z\d]+)'.*?@([\w]+)")
    match = pattern.search(s)
    if match:
        print match.group(1)
        print(match.group(2))
        print match.group(3)
        # print(match.group())
re_test()

def re_test():
    s =  "select sysdate from dual where user= '@user1' and password='@password' or username='@username'"
    pattern = re.compile("'.*?@(?P<user>([a-z\d]+))'.*?'@(?P<passwd>([a-z\d]+))'.*?@(?P<username>([\w]+))")
    match = pattern.search(s)
    if match:
        print(match.group(1))
        print(match.group(2))
        print(match.group(3))
        print match.group('user')
        print(match.group('passwd'))
        print match.group('username')
        print(match.group())
re_test()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值