Python正则小实例
2014-03-31 20:16
363 查看
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()
相关文章推荐
- Python3爬虫简易实例1(Requests正则)
- python 正则实例
- Python正则表达式使用实例
- python正则表达式--基本用法和函数(简单实例)
- Python中正则表达式的用法实例汇总
- python 运用requests库 以及 正则表达式 爬取小说实例以及心得
- Python正则表达式匹配ip地址实例
- python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
- python 3.6 正则表达式的应用(附实例代码)
- Python 正则表达式---概念基础,匹配过程,表达式元字符和语法以及实例
- Python正则简单实例分析
- Python爬虫实例(4)-用urllib、re和正则表达式爬取网页图片
- Python正则表达式匹配ip地址实例
- Python正则表达式如何进行字符串替换实例
- 基于正则表达式(python)对东方财富网上证指数吧爬虫实例
- python 用正则表达式筛选文本信息的实例
- python类库31[正则表达式匹配实例]
- python正则表达式match和search用法实例
- python正则捕获日志解析实例
- python re正则表达式使用实例