Odoo中文网|Odoo实施培训

标题: Python中re.match与re.search的区别 [打印本页]

作者: 深圳-马话疼    时间: 2015-11-21 23:53
标题: Python中re.match与re.search的区别
Python的re模块提供Perl风格的正则表达式模式,re模块使Python语言拥有全部的正则表达式功能。
re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
实例:

  1. #!/usr/bin/python
  2. import re

  3. line = "Cats are smarter than dogs";

  4. matchObj = re.match( r'dogs', line, re.M|re.I)
  5. if matchObj:
  6.    print "match --> matchObj.group() : ", matchObj.group()
  7. else:
  8.    print "No match!!"

  9. matchObj = re.search( r'dogs', line, re.M|re.I)
  10. if matchObj:
  11.    print "search --> matchObj.group() : ", matchObj.group()
  12. else:
  13.    print "No match!!"
复制代码
以上实例运行结果如下:
No match!!
search --> matchObj.group() :  dogs


作者: smlqf1    时间: 2018-8-27 01:58
顶上去!顶上去!











簡歷封面   http://www.gfgfgf.com.tw/zybj/xljk/   2018年08月27日 家常菜譜
中醫疾病    一句話經典語錄 http://www.gfgfgf.com.tw/zybj/qjys/  男士短髮   中醫足療       http://www.gfgfgf.com.tw/zycs/wwwq/  生日快樂祝賀詞




欢迎光临 Odoo中文网|Odoo实施培训 (http://www.chinaodoo.net/) Powered by Discuz! X3.2