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

    # 正则模块
    '''
    正则就是用一些具有特殊含义的符号组合到一起用来描述字符或字符串的方法
    或者说,正则就是用来描述一类事物的规则
    它内嵌在python中,并通过re模块实现
    正则表达式模式被编译成一系列的字节码,然后由用c编写的匹配引擎执行
    '''
    '''
    生活中处处都是正则
    比如,描述:四条腿
    四条腿的动物或者桌子椅子
    继续描述:活的
    四条腿的动物
    '''
    '''
    w 匹配字母数字及下划线
    W 匹配非字母数字及下划线
    s 匹配任意空白字符
    S 匹配任意空白字符
    d 匹配任意数字
    D 匹配任意非数字
    匹配一个换行符
    匹配一个制表符
    ^ 匹配字符串的开头
    $ 匹配字符串的末尾
    '''
    # import re
    # print(re.findall('w','egon 123 + - _ *')) # ['e', 'g', 'o', 'n', '1', '2', '3', '_']
    # print(re.findall('W','egon 123 + - _ *')) # [' ', ' ', '+', ' ', '-', ' ', ' ', '*']
    # print(re.findall('s','egon 123 + - _ *')) # [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
    # print(re.findall('S','egon 123 + - _ *')) # ['e', 'g', 'o', 'n', '1', '2', '3', '+', '-', '_', '*']
    # print(re.findall('d','egon 123 + - _ *')) # ['1', '2', '3']
    # print(re.findall('D','egon 123 + - _ *')) # ['e', 'g', 'o', 'n', ' ', ' ', '+', ' ', ' ', ' ', ' ', ' ', '-', ' ', '_', ' ', '*']
    # print(re.findall(' ','egon 123 + - _ *')) # [' ']
    # print(re.findall(' ','egon 123 + - _ *')) # [' ']
    # print(re.findall('e','egon 123 + - e _ *')) #['e', 'e']
    # print(re.findall('^e','egon 123 + - _ *')) # ['e']
    # print(re.findall('^e','aegon 123 + - _ *')) # []
    # print(re.findall('o','egon 123 + hello')) # ['o', 'o']
    # print(re.findall('o$','egon 123 + hello')) # ['o']
    # print(re.findall('o$','egon 123 + helloa')) # []
    '''
    . 代表任意一个字符 不能匹配换行符
    ? 代表?左边的字符出现0次或者1次
    * 代表*号左边的字符出现0次或者无穷次
    + 代表+号左边的字符出现1次或者无穷次
    {m,n} 代表左边的字符出现m次到n次
    .* 贪婪匹配 匹配不够标准 一般是有害的
    .*? 非 贪婪匹配
    '''
    # print(re.findall('a.b','a1b a b a-b aaaaaaaaabbbb')) # ['a1b', 'a b', 'a-b', 'aab']
    # print(re.findall('a.b','a1b a b a b a-b aaaaaaaaabbbb')) # ['a1b', 'a b', 'a-b', 'aab']
    # print(re.findall('a.b','a1b a b a b a-b aaaaaaaaabbbb',re.DOTALL)) # ['a1b', 'a b', 'a b', 'a-b', 'aab']
    # print(re.findall('ab?','a ab a1b a b a b a-b aaaaaaaaabbbb')) # ['a', 'ab', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'ab']
    # print(re.findall('ab*','a ab a1b a b a b a-b aaaaaaaaabbbb')) # ['a', 'ab', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'abbbb']
    # print(re.findall('ab+','a ab a1b a b a b a-b aaaaaaaaabbbb')) # ['ab', 'abbbb']
    # print(re.findall('ab?','a ab a1b a b a b a-b aaaaaaaaabbbb'))
    # print(re.findall('ab{0,1}','a ab a1b a b a b a-b aaaaaaaaabbbb'))
    '''
    ['a', 'ab', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'ab']
    ['a', 'ab', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'ab']
    '''
    # print(re.findall('ab*','a ab a1b a b a b a-b aaaaaaaaabbbb'))
    # print(re.findall('ab{0,}','a ab a1b a b a b a-b aaaaaaaaabbbb'))
    '''
    ['a', 'ab', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'abbbb']
    ['a', 'ab', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'abbbb']
    '''
    # print(re.findall('ab+','a ab a1b a b a b a-b aaaaaaaaabbbb'))
    # print(re.findall('ab{1,}','a ab a1b a b a b a-b aaaaaaaaabbbb'))
    '''
    ['ab', 'abbbb']
    ['ab', 'abbbb']
    '''
    # print(re.findall('ab{1,4}','a ab a1b abbbbb b a b a-b aaaaaaaaabbbb')) # ['ab', 'abbbb', 'abbbb']
    # print(re.findall('a.*b','a ab xxxyya123b345b a123ba456b'))
    # print(re.findall('a.*b','xxxyya123b345b a123ba456b'))
    '''
    ['a ab xxxyya123b345b a123ba456b']
    ['a123b345b a123ba456b']
    '''
    # print(re.findall('a.*?b','a ab xxxyya123b345b a123ba456b'))
    # print(re.findall('a.*?b','xxxyya123b345b a123ba456b'))
    '''
    ['a ab', 'a123b', 'a123b', 'a456b']
    ['a123b', 'a123b', 'a456b']
    '''
    # print(re.findall('compan(y|iess)','too many companiess have gone company bankrupt,and the next cpompan one is whos company'))
    # print(re.findall('compan(?:y|iess)','too many companiess have gone company bankrupt,and the next cpompan one is whos company'))
    '''
    ['iess', 'y', 'y']
    ['companiess', 'company', 'company']
    '''
    # print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击我</a>'))
    '''
    ['http://www.baidu.com']
    '''
    # rawstring
    # print(re.findall(r'a\c','ac a1c abc'))
    # print(re.findall('a\c','ac a1c abc'))
    # print(re.findall('a\\c','ac a1c abc'))
    '''
    ['a\c']
    ['a\c']
    ['a\c']
    '''
    # [] 取[]内任意的一个
    # print(re.findall('a[a-z]b','acb aDb a-b aob a1b a')) # ['acb', 'aob']
    # print(re.findall('a[A-Z]b','acb aDb a-b aob a1b a')) # ['aDb']
    # print(re.findall('a[a-zA-Z]b','acb aDb a-b aob a1b a')) # ['acb', 'aDb', 'aob']
    # print(re.findall('a[0-9]b','acb aDb a-b aob a1b a')) # ['a1b']
    # print(re.findall('a[-+*/]b','acb aDb a-b aob a1b a')) # ['a-b']

    # print(re.findall('a[^-+*/]b','acb aDb a-b aob a1b a')) # ['acb', 'aDb', 'aob', 'a1b']
    # print(re.findall(r'a[^-+*/]b','acb aDb a-b aob a1b a'))
    # print(re.findall('ra[^-+*/]b','acb aDb a-b aob a1b a'))
    # print(re.findall('a[^-+*/]b',r'acb aDb a-b aob a1b a'))
    # print(re.findall('a[^-+*/]b','acb aDb a-b aob a1b a\b'))
    '''
    ['acb', 'aDb', 'aob', 'a1b']
    ['acb', 'aDb', 'aob', 'a1b']
    []
    ['acb', 'aDb', 'aob', 'a1b', 'a\b']
    ['acb', 'aDb', 'aob', 'a1b', 'a\b']
    '''

    # re模块的其他方法
    # re.search() 只取一个
    # print(re.search('a[^-+*/]b','acb aDb a-b aob a1b a9b a'))
    # print(re.search('a[*]b','acb aDb a-b aob a1b a9b a'))
    # print(re.search('a[0-9]b','acb aDb a-b aob a1b a9b a'))
    # print(re.search('a[0-9]b','acb aDb a-b aob a1b a9b a').group())
    '''
    <re.Match object; span=(0, 3), match='acb'>
    None
    <re.Match object; span=(16, 19), match='a1b'>
    a1b
    '''
    # re.match()
    # print(re.match('a[0-9]b','a0b acb aDb a-b aob a1b a9b').group()) # a0b
    # print(re.search('^a[0-9]b','a0b acb aDb a-b aob a1b a9b').group()) # a0b
    # re.split()
    # print(re.split(':','root:x:0:0::/root:/bin/bash')) # ['root', 'x', '0', '0', '', '/root', '/bin/bash']
    # print(re.split(':','root:x:0:0::/root:/bin/bash',maxsplit=1)) # ['root', 'x:0:0::/root:/bin/bash']
    # re.sub() 替换
    # print(re.sub('root','admin','root:x:0:0::/root:/bin/bash')) # admin:x:0:0::/admin:/bin/bash
    # print(re.sub('root','admin','root:x:0:0::/root:/bin/bash',1)) # admin:x:0:0::/root:/bin/bash
    # print(re.sub('^([a-zA-Z]+)([^a-zA-Z])(.*?)([^a-zA-Z])([a-zA-Z]+)$',r'52341','root:x:0:0::/root:/bin/bash')) # bash:x:0:0::/root:/bin/root
    # re.compile()
    # import re
    # obj=re.compile('ad{2}b')
    # print(obj.findall('a12b a123b a12345b abbb')) # ['a12b']
    # print(obj.search('a12b a123b a12345b abbb'))
    # print(obj.search('a12b a123b a12345b abbb').group())
    '''
    <re.Match object; span=(0, 4), match='a12b'>
    a12b
    '''
    # import time
    # print(time.time()) # 1576152639.0129972 时间戳 一般用来计量时间
    # print(time.localtime()) # 本地时区 东八区
    # print(time.gmtime()) # 标准时间
    '''
    time.struct_time(tm_year=2019, tm_mon=12, tm_mday=12, tm_hour=20, tm_min=12, tm_sec=26, tm_wday=3, tm_yday=346, tm_isdst=0)
    time.struct_time(tm_year=2019, tm_mon=12, tm_mday=12, tm_hour=12, tm_min=12, tm_sec=26, tm_wday=3, tm_yday=346, tm_isdst=0)
    '''
    # print(time.strftime('%Y-%m-%d %H-%M-%S'))
    # print(time.strftime('%Y-%m-%d %X'))
    '''
    2019-12-12 20-16-10
    2019-12-12 20:16:10
    '''
    # 了解
    # print(time.localtime(time.time())) # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=12, tm_hour=20, tm_min=20, tm_sec=13, tm_wday=3, tm_yday=346, tm_isdst=0)
    # print(time.gmtime(time.time())) # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=12, tm_hour=12, tm_min=21, tm_sec=51, tm_wday=3, tm_yday=346, tm_isdst=0)
    # print(time.mktime(time.localtime())) # 1576153398.0
    # print(time.strftime('%Y-%m-%d',time.localtime())) # 2019-12-12
    # print(time.strptime('2019-12-12','%Y-%m-%d')) # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=346, tm_isdst=-1)
    # print(time.asctime(time.localtime()))
    # print(time.ctime())
    '''
    Thu Dec 12 20:28:03 2019
    Thu Dec 12 20:28:03 2019
    '''
    # datetime
    # import datetime
    # print(datetime.datetime.now()) # 2019-12-12 20:30:13.626072
    # print(datetime.datetime.fromtimestamp(111111)) # 1970-01-02 14:51:51
    # print(datetime.datetime.now()+datetime.timedelta(days=3))
    # print(datetime.datetime.now()+datetime.timedelta(days=-3))
    # print(datetime.datetime.now()+datetime.timedelta(hours=3))
    # print(datetime.datetime.now()+datetime.timedelta(minutes=3))
    # print(datetime.datetime.now()+datetime.timedelta(seconds=3))
    '''
    2019-12-15 20:34:16.275172
    2019-12-09 20:34:16.275172
    2019-12-12 23:34:16.275172
    2019-12-12 20:37:16.275172
    2019-12-12 20:34:19.275172
    '''
    # print(datetime.datetime.now().replace(year=1999,hour=12)) # 1999-12-12 12:35:44.337966
  • 相关阅读:
    【转载】分析商品日均销量(DMS)对促销商品选择的意义
    日志备份和差异备份还原中的常见问题示例(转自&邹建)
    SQL Server 2000中的完整备份、差异备份操作
    数据库差异备份与增量备份的不同之处
    差异备份和还原操作方法(转)
    SQL备份(全)
    Microsoft SQL2000 错误代码 (@@error)
    图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别
    arm-none-linux-gnueabi-gcc command not found
    关于ST-Link的internal command error问题的解决方法
  • 原文地址:https://www.cnblogs.com/0B0S/p/12031480.html
Copyright © 2011-2022 走看看