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

    re模块:从字符串里面找到特定的字符串

    re的基本语法(匹配规则)

    import re
    s = '王大炮打炮被大炮打死了 王大炮打炮被大炮打死了'

    ^:开头

    print(re.findall('^王大炮',s))

    $:结尾

    print(re.findall('死了$',s))

    []匹配中间的字符,只要单个字符

    s = 'acefghjkacefsdfsdf'
    print(re.findall('[acef]',s))

    []+连用,对[]内的元素取反

    print(re.findall('[^acef]',s))

    .任意字符除了

    s='abacadaeaf'
    print(re.findall('a..',s))

    s = 'abaacaaaaa'

    *前面的字符0-无穷个

    print(re.findall('a*',s))

    +前面的字符1-无穷个

    print(re.findall('a+',s))

    ?前面的字符0-1个

    print(re.findall('a?',s))

    {m}前面的字符m个

    print(re.findall('a{5}',s))

    {n,m}前面的字符n-m个

    print(re.findall('a{2,5}',s))

    s='s 1 s+ =$ 2_s 3'

    d数字

    print(re.findall('d',s))

    D非数字

    print(re.findall('D',s))

    w数字字母下划线

    print(re.findall('w',s))

    W非数字字母下划线

    print(re.findall('W',s))

    s空格

    print(re.findall('s',s))

    S非空格

    print(re.findall('S',s))

    取消意义

    s='abad'
    print(re.findall(r'a',s))

    .*贪婪模式,找到值后继续找到最后,让结果最大化

    s='abbbcabc'
    print(re.findall('a.*c',s))

    .*?非贪婪模式,找到第一个后就马上停止了

    print(re.findall('a.*?c',s))

    ()只要括号内的除了什么以外

    s='abacad'
    print(re.findall('a(.)',s))

    A|B,A和B都要

    s='abacad'
    print(re.findall('a|b',s))

  • 相关阅读:
    fn project 试用之后的几个问题的解答
    fn project 扩展
    fn project 生产环境使用
    fn project 对象模型
    fn project AWS Lambda 格式 functions
    fn project 打包Function
    fn project Function files 说明
    fn project hot functions 说明
    fn project k8s 集成
    fn project 私有镜像发布
  • 原文地址:https://www.cnblogs.com/ludundun/p/11382970.html
Copyright © 2011-2022 走看看