zoukankan      html  css  js  c++  java
  • 正则表达式,re模块

    一.正则表达式

      正则表达式 : 匹配字符串,一般用于爬取数据. 

      正则表达式查询网址 : http://tool.chinaz.com/regex/?qq-pf-to=pcqq.group

      1.元字符(常用的)

         .     匹配除了换行符以外的所有字符

        w    匹配 数字 字母 下划线  (大写W,表示非)

        d     匹配 数字  (大写D,表示非)

        s     匹配 空白字符  (大写S,表示非)

             匹配 单词的边界   (大写B,表示非)

        []    字符组,一个字符

        ^    从字符串的开始匹配

        $    字符串的结尾

      2.量词(常用的)

        *    零次或者多次{0,}

        +    一次或者多次{1,}

        ?    零次或者一次{0,1}

        {n,m}   n至m次 

      3.贪婪

          .*    贪婪,尽可能多的匹配

        .+    贪婪,尽可能多的匹配

           .*?    非贪婪,尽可能少的匹配

    二.re模块

      re.findall()     查找所有匹配结果,返回一个列表

      re.finditer()    查找所有匹配结果,返回一个迭代器 需要用group("name")方法来拿数据

      re.search()    搜索, 只要找到了就立即返回,没有找到返回None

      re.match()     匹配,收到一个结果,不过是从字符串开头找,找不到就返回None

      re.compile()     编译,把正则表达式预编译

      re.S       flag 的参数,可以使 . 匹配到所有东西

           

  • 相关阅读:
    Leetcode Binary Tree Level Order Traversal
    Leetcode Symmetric Tree
    Leetcode Same Tree
    Leetcode Unique Paths
    Leetcode Populating Next Right Pointers in Each Node
    Leetcode Maximum Depth of Binary Tree
    Leetcode Minimum Path Sum
    Leetcode Merge Two Sorted Lists
    Leetcode Climbing Stairs
    Leetcode Triangle
  • 原文地址:https://www.cnblogs.com/q767498226/p/10192648.html
Copyright © 2011-2022 走看看