Python中使用正则表达式
2012-07-22 17:39
537 查看
转自:/article/3535286.html
《Python unix与linux系统管理指南》学习笔记
Python中使用正则表达式,应该要养成创建编译后的正则表达式的习惯,使用方法如下:
常用的正则表达式方法有findall(), finditer(), match(), search()
《Python unix与linux系统管理指南》学习笔记
Python中使用正则表达式,应该要养成创建编译后的正则表达式的习惯,使用方法如下:
#!/usr/bin/env python import re def run_re(): pattern = 'ERROR' re_obj = re.compile(pattern) infile = open('/home/udb/jt.txt', 'r') match_count = 0 lines = 0 for line in infile: match = re_obj.search(line) if match: match_count += 1 lines += 1 return (lines, match_count) if __name__ == "__main__": lines, match_count = run_re() print 'LINES--->', lines print 'MATCHES--->', match_count
常用的正则表达式方法有findall(), finditer(), match(), search()
相关文章推荐
- python使用正则表达式检测密码强度源码分享
- Python正则表达式的七个使用范例
- python里使用正则表达式的详细模式(Verbose mode expression)
- Python中正则表达式的使用方法
- python正则表达式使用
- [python爬虫] 正则表达式使用技巧及爬取个人博客实例
- python 使用正则表达式的爬虫
- Python正则表达式使用范例分享
- 比较详细Python正则表达式操作指南(re使用)[6]
- 基于python的-使用正则表达式验证手机号
- python里使用正则表达式来分割字符串
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
- Python正则表达式--Re库的基本使用
- 比较详细Python正则表达式操作指南(re使用)
- 在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类
- python 详解正则表达式的使用(re模块)
- Python爬虫之正则表达式的使用教程详解
- python里使用正则表达式来替换匹配成功的组名
- 使用正则表达式替换文件内容 分类: python 小练习 2013-08-13 15:07 332人阅读 评论(0) 收藏
- Python::re 模块 -- 在Python中使用正则表达式