zoukankan      html  css  js  c++  java
  • re 模块

    1、re.match  

       re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()返回none。

       re.match(pattern,string,flags=0)

         最常规的匹配

      import re

      content ='hello 123 4567 World_This is a Regex Demo'

      result = re.match('^hellosd[4]sw[10].*Demo$')

      print(result)

      print(result.group())

      print(result.span())

     泛匹配

     import re

        content ='hello 123 4567 World_This is a Regex Demo'

     result =re.match('^hello.*Demo$',content)

        print(result)

        print(result.group())  //结果

      匹配目标

      import re

        content ='hello 123 4567 World_This is a Regex Demo'

      result=re.match('^Hellos(d+)sWorld.*Demo$',content)

      print(result.group(1))  //取第一个小括号中的内容。

         贪婪匹配

      import re

      content ='hello 123 4567 World_This is a Regex Demo'

      result =re.match('^He.*(d+).*Demo$',content)

      print(result。group(1)) // 结果 7

     

         非贪婪匹配    

       import re

       content ='hello 123 4567 World_This is a Regex Demo'

       result =re.match('^He.*?(d+).*Demo$',content)

       print(result.group(1))  //结果1234567

      匹配模式

      import re

        content ='Hello 123 4567 World_This

           is a Regex Demo'    //有换行

           result =re.match('^He.*?(d+).*?Demo$',content,re.S)   //可以匹配换行符

      print(result.group(1))  //1234567

           转义

      import re

         content ='price is $5.00'

       result = re.match('price is $50.00',content)

            print(result)   //结果 None

      转 

      import re

         content ='price is $5.00'

       result = re.match('price is $50.00',content)

            print(result)

    2、re.search

      re.search 扫描证儿歌字符串并返回第一个成功的匹配

      import re

      content = 'Extra strings Hello 12345678 World_This is a Regex Demo Extra strings'

      result = re.search('Hello.*?(d+).*?Demo',content)   //对开头没有必要的要求

      print(result.group(1))

    3、re.findall

      搜索字符串,搜索所有符合条件的字符串。

  • 相关阅读:
    几篇操作Excel的文章资源
    [转]Oracle用一个表的列更新另一个表对应记录的列
    【转】Earth Viewers几个三维地球软件比较
    在ArcGIS Server中如何定义“此级别无数据”图片?的终极解决方案
    [转]ODAC 应用技巧 (一)使用 ODAC 的 Net 方式
    Centos7 ipset命令介绍及使用
    在线编辑Word——插入图表 E
    Java 在PPT中创建散点图 E
    yum问题Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again
    Base64和urlencode
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7789049.html
Copyright © 2011-2022 走看看