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

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

  • 相关阅读:
    Linux-Rsync命令参数详解
    Linux-iptables(2)
    Linux-iptables
    Linux-awk command
    Linux-sed command
    Linux-tomcat
    C#调用默认浏览器打开网页的几种方法
    个人记录用
    .NET中的Request
    sql标量值函数,将汉字转化为拼音,无音标
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7789049.html
Copyright © 2011-2022 走看看