Python正则小实例

来源:互联网 发布:淘宝ppc和cpc的区别 编辑:程序博客网 时间:2024/06/14 10:52
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()

0 0
原创粉丝点击