zoukankan      html  css  js  c++  java
  • PYTHON_正则表达式

    字符匹配方法

    在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。

    通配符:*

    元字符:    ^    $   *    +     .    |    ?    {}    []    ()

    ^ 表示匹配字符串的开头。在多行模式下匹配每一行的开头

    $ 表示匹配字符串的结尾。在多行模式下匹配每一行的尾部。

     反斜杠后面可以加不同的字符以表示特殊意义,d,D,s,S,w,W

    重复匹配字符(匹配符号前字符,匹配次数代表能够匹配到的字符数量):*    +     ?    {}__指定匹配字符数量

    其它匹配字符 () | .

    组合 .* .*? (.*?)

    符号组合(.*?)

    方法:findall,search,sub

    re.findall(匹配规则,待匹配项)

    1 res = r'^hello'      #匹配规则      ^
    2 s = 'hello world ,hello python hello boy'      #待匹配项
    3 re.findall(res,s)

    原则:先抓大再抓小

    findall 匹配所有内容,返回列表 re.findall(pattern, string, flags=0)

    search 匹配第1个内容,返回对象 re.search(pattern, string, flags=0)

    sub 替换所有内容,返回替换后值 re.sub(pattern, repl, string, count=0, flags=0)

  • 相关阅读:
    初识ambari
    MySQL Split 函数
    行存储和列存储
    Hbase安装和错误
    mysql 常用自定义函数解析
    mysq l错误Table ‘./mysql/proc’ is marked as crashed and should be repaired
    MySql提示:The server quit without updating PID file(…)失败
    mysql 自定义函数
    hive 调优总结
    [css] line boxes
  • 原文地址:https://www.cnblogs.com/wilson297/p/6445279.html
Copyright © 2011-2022 走看看