zoukankan      html  css  js  c++  java
  • re.match与re.search的区别

    re.match与re.search的区别

    re.match 只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回 None,而 re.search 匹配整个字符串,直到找到一个匹配。

    实例

    #!/usr/bin/python3
    import re
    line = "Cats are smarter than dogs"
    matchObj = re.match( r'dogs', line, re.M|re.I)
    if matchObj:
      print ("match --> matchObj.group() : ", matchObj.group())
    else:
      print ("No match!!")
    matchObj = re.search( r'dogs', line, re.M|re.I)
    if matchObj:
      print ("search --> matchObj.group() : ", matchObj.group())
    else:
      print ("No match!!")

    以上实例运行结果如下:

    No match!!
    search --> matchObj.group() :  dogs
  • 相关阅读:
    ReentrantLock与synchronized的差别
    读TIJ -1 对象入门
    wikioi 2573 大顶堆与小顶堆并用
    开源 免费 java CMS
    UVA10972
    springboot5
    spring-boot4
    spring-boot3
    spring-boot2
    spring-boot1
  • 原文地址:https://www.cnblogs.com/wangdayang/p/14914842.html
Copyright © 2011-2022 走看看