python的re模块应用实例
2014-09-26 11:31
621 查看
import re # match_object = re.match('foo','foo') if match_object is not None: print type(match_object) print match_object.group() # match_object = re.match('foo','fooabv') if match_object is not None: print match_object.group() #match从头开始匹配 match_object = re.match('foo','afooabv') if match_object is not None: print match_object.group() else: print 'not match' #利用面向对象的特点,一行完成 print re.match('love','lovesomebody is a happy thing').group() #与match的区别:match从头开始匹配,search是查找 match_object = re.search('foo','afooabv') if match_object is not None: print match_object.group() else: print 'not match' #|的使用 bt = 'bat|bit|bot' match_object = re.match(bt,'batsdf') if match_object is not None: print "|...|" + match_object.group()#会匹配成功 else: print 'not match' bt = 'bat|bit|bot' match_object = re.search(bt,'aabatsdf') if match_object is not None: print "|search|" + match_object.group()#会匹配成功,如果用match就不会匹配成功 else: print 'not match'
本文实例测试环境为Python2.7.6
运行结果如下:
<type '_sre.SRE_Match'> foo foo not match love foo |...|bat |search|bat
希望本文所述对大家的Python程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- python的re模块应用实例
- python应用regex正则表达式模块re
- [转]Python中RE模块的应用
- Python高级——正则表达式 re模块 2.高级应用
- Python中RE模块的应用
- [转]Python中RE模块的应用
- python应用regex正则表达式模块re
- [转]Python中RE模块的应用
- python全栈开发-re模块(正则表达式)应用(字符串的处理)
- [转] Python中RE模块的应用
- Python中RE模块的应用
- Python 3.x--使用re模块,实现计算器运算实例
- python的re模块
- Python模块学习 re 正则表达式
- 手把手教你写S12XS128程序(25)--Timer模块应用实例2
- python的实例应用
- PYTHON正则表达式 re模块使用说明
- 深入探讨Python ConfigParser模块正确应用方式
- python 应用实例
- Python模块之RE