python的re模块应用实例

系统 1391 0

本文实例讲述了python的re模块应用。是非常重要的应用技巧。分享给大家供大家参考。

具体方法如下:

            
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

运行结果如下:

            
              
foo
foo
not match
love
foo
|...|bat
|search|bat


            
          

希望本文所述对大家的Python程序设计有所帮助。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论