zoukankan      html  css  js  c++  java
  • re

    # import re
    '''all=[
    "match", "fullmatch", "search", "sub", "subn", "split",
    "findall", "finditer", "compile", "purge", "template", "escape",
    "error", "A", "I", "L", "M", "S", "X", "U",
    "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
    "UNICODE",
    ]'''
    '''
    match Match a regular expression pattern to the beginning of a string.
    fullmatch Match a regular expression pattern to all of a string.
    search Search a string for the presence of a pattern.
    sub Substitute occurrences of a pattern found in a string.
    subn Same as sub, but also return the number of substitutions made.
    split Split a string by the occurrences of a pattern.
    findall Find all occurrences of a pattern in a string.
    finditer Return an iterator yielding a match object for each match.
    compile Compile a pattern into a RegexObject.
    purge Clear the regular expression cache.
    escape Backslash all non-alphanumerics in a string.
    '''
    # import re
    # content = 'Hello 1234567 World_This is a Demo'
    # result = re.match('Hellos(d+)sWorld',content)
    # print(result)
    # print(result.group())
    # print(result.group(1))
    # print(result.span())
    # #结果如下:
    # '''
    # <_sre.SRE_Match object; span=(0, 19), match='Hello 1234567 World'>
    # Hello 1234567 World
    # 1234567
    # (0, 19)
    # '''
    # import re
    #
    # content = 'Hello 1234567 World_This is a Demo'
    # r = re.match('^Hello.*Demo$', content)
    # print(r)
    # print(r.group())
    # print(r.span())
    #
    # # 结果如下:
    # '''
    # <_sre.SRE_Match object; span=(0, 34), match='Hello 1234567 World_This is a Demo'>
    # Hello 1234567 World_This is a Demo
    # (0, 34)
    # '''


    # #贪婪 .*
    # import re
    #
    # content = 'Hello 1234567 World_This is a Demo'
    # r = re.match('^He.*(d+).*Demo$',content)
    # print(r)
    # print(r.group(1))
    # #结果如下:
    # '''
    # <_sre.SRE_Match object; span=(0, 34), match='Hello 1234567 World_This is a Demo'>
    # 7
    # '''
    #
    # #我们只得到一个结果7,在贪婪匹配下,.* 会匹配尽可能多的字符。而d+至少匹配一个字符。也就是说.* 把123456都匹配了,只留下7给d+

    # #非贪婪 .*?
    # import re
    #
    # content = 'Hello 1234567 World_This is a Demo'
    # r = re.match('^He.*?(d+).*Demo$',content)
    # print(r)
    # print(r.group(1))
    #
    # #结果如下:
    # '''
    # <_sre.SRE_Match object; span=(0, 34), match='Hello 1234567 World_This is a Demo'>
    # 1234567
    # '''
    # # 我们只得到一个结果1234567,在非贪婪匹配下,.* ?会匹配尽可能少的字符。当.* ?匹配到Hello后面的空白字符时,再往后就是数字,而d+正好可以匹配到。也就是说.* ?把数字都留给d+
    #

    # #re.S (大写的S) 换行
    # import re
    #
    # content = '''Hello 1234567 World_This
    # is
    # a Demo'''
    # r = re.match('^He.*?(d+).*Demo$',content,re.S)#不加re.S则报错
    # print(r)
    # print(r.group(1))
    '''import re
    re.I ====>使匹配对大小写不敏感
    re.L ====>做本地化识别(locale-aware)匹配
    re.M ====>多行匹配,影响^和$
    re.S ====>使.匹配包括换行在内的所有字符
    re.U ====>根据Unicode字符集解析字符。这个标志影响w W  B
    re.X ====>该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解
    '''

    # # 转义匹配 /
    # import re
    # content = '(百度) http:\www.baidu.com'
    # r= re.match('(百度)\ http:\\www.baidu.com',content)
    # print(r)


    # #search() 匹配所有符合中的第一个内容
    # import re
    #
    # content = 'FEDfs Hello 1234567 World_This is a Demo'
    # #match()是从第一个字符开始匹配
    # r = re.match('He.*?(d+).*Demo$',content)#结果为:None
    # q=re.search('He.*?(d+).*Demo$',content)#结果为:<_sre.SRE_Match object; span=(1, 35), match='Hello 1234567 World_This is a Demo'>
    # print(r,q)


    # import re
    # #匹配所有符合的内容,返回list,for循环得到每一条结果
    # re.findall()


    # #sub()除正则表达式以外的内容
    # import re
    # content = 'dfjasily2q3uy67wqehfdjiay7u823q6'#不想要数字
    # r= re.sub('d+','',content)
    # print(r)

    # #compile()将正则表达式编译为对象,方便以后重复使用
    # import re
    # c1='2019-05-03 12:55'
    # c2='2019-06-03 02:25'
    # c3='2019-07-03 18:44'
    # pattern = re.compile('dd:d{2}')
    # r1=re.sub(pattern,'',c1)
    # r2=re.sub(pattern,'',c2)
    # r3=re.sub(pattern,'',c3)
    # print(r1,r2,r3)
  • 相关阅读:
    文字编码转换器 V1.0 免费绿色版
    一把刀系统维护工具箱 v1.6 绿色版
    一把刀终极配置Win7/8版 v2.0 绿色版
    移动端 iframe的使用
    scale配合过渡的时候bug
    js 简体中文拼音对应表
    原生js快速渲染dom节点
    让我们的svg起飞,兼容ie9的神器
    盒模型
    Normalize.css 与 reset.css
  • 原文地址:https://www.cnblogs.com/tootise/p/10918505.html
Copyright © 2011-2022 走看看