• 正交表达式


    '''
    匹配原则,匹配的数据
    只能够接受字符串
    '''
    import re
    m=re.match('abc','abcdfe')
    print(m.group())
    o=re.match('[0-9][0-9]','567ab123c5')#[0-9]只代表一个数字,这样子代表匹配两个数字
    p=re.match('[0-9]{0,2}','3456ablc123d')#匹配【0-9】2位,一数字开头的,后面的不会被匹配,从头找,匹配到就返回
    i=re.match('[0-9]{10}','1234abc') #匹配10位,这样会匹配失败
    a=re.findall('[0-9]','a1d3f3d1a3d4f')
    b=re.findall('[0-9]{0,8}','a1d344444444444f3ggfgfgdfdfdd1a3d4f')
    #后面的表示{}表示匹配多少位,如果是8,表示匹配8位,表示0位到8位的数据都满足要求
    c=re.findall('[0-9]{8}','a1d344444444444f3ggfgfgdfdfdd1a3d4f')
    #要求必须是8位
    d=re.findall('.*','a1d344444444444f3ggfgfgdfdfdd1a3d4f')
    f=re.findall('.','''a1d344444444444f3ggfgfgdfdfdd1a3d4f
    sdfdsdfdfdfdsf''')#p匹配单行所有
    print(o)
    f=re.findall('ab*',"""a1d344444444444f3ggfgfgdfdfdd1a3d4f
    sdfdsdfdfdfbdsf""")#匹配多行,必须是以a开头,可以是a
    #可以是ab
    G=re.findall('ab+','''a1d344444444444f3ggfgfgdfdfdd1a3d4f
    sdfdsdfdfdfdsf''')#匹配ab必须连在一起,匹配多个
    i=re.findall('ab+','''ab1d34444444444ab4f3ggfgfgdfdfdd1a3d4f
    sdfdsdfdafdfdabcsf''')#匹配ab必须连在一起,匹配多个
    H=re.findall('S','''a1d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$fdsf''')
    j=re.findall('~','''a1d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$fdsf''')

    k=re.search('d+','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf''')
    l=re.sub('d','G','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf''')#会把所有的数字替换成大写的G
    m=re.sub('d','G','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf''',count=2)# 找到前两个进行替换
    Q=re.sub('^d','G','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf''',count=2)# 找到一数字开头,这次会失败
    n=re.search('^d','''3a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf''')# 找到一数字开头,这次会成功
    N=re.search('^d+$','''a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf''')# 找到一数字开头,以数字结尾
    print(o)
    print('ogoup',o.group())
    print(p)
    print(p.group())
    print(i)
    print(a)
    print('c',c)
    print('b',b)
    print(d)
    print(f)
    print('g',G)
    print('s',H)
    print('i',i)
    print('j',j)
    print('k',k)
    print('l',l)
    print('m',m)
    print('o',Q)
    print('n',n)
    print('N',N)


    结果:

    C:Python34python.exe "C:/Users/Admin/PycharmProjects/untitled1/第5 天/正交表达式.py"
    abc
    <_sre.SRE_Match object; span=(0, 2), match='56'>
    <_sre.SRE_Match object; span=(0, 2), match='56'>
    ogoup 56
    <_sre.SRE_Match object; span=(0, 2), match='34'>
    34
    ['ab', 'ab', 'ab']
    ['1', '3', '3', '1', '3', '4']
    c ['34444444']
    b ['', '1', '', '34444444', '4444', '', '3', '', '', '', '', '', '', '', '', '', '', '', '', '1', '', '3', '', '4', '', '']
    ['a1d344444444444f3ggfgfgdfdfdd1a3d4f', '']
    ['a', 'a']
    g []
    s ['a', '1', 'd', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '~', '!', '4', 'f', '3', 'g', 'g', 'f', 'g', 'f', 'g', 'd', '#', '#', 'f', 'd', 'f', 'd', 'd', '1', 'a', '3', 'd', '4', 'f', 's', 'd', 'f', 'd', 's', 'd', 'f', 'd', 'f', 'd', 'f', 'd', 'f', '@', '#', '$', 'f', 'd', 's', 'f']
    i ['ab', 'ab', 'ab']
    j ['~']
    k <_sre.SRE_Match object; span=(1, 3), match='34'>
    l aGGdGGGGGGGGGGG~!GfGggfgfgd## fdfddGaGdGf
    sdfdsdfdfdfdf@#$GG.Gfdsf
    m aGGd34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf
    o a34d34444444444~!4f3ggfgfgd## fdfdd1a3d4f
    sdfdsdfdfdfdf@#$46.5fdsf
    n <_sre.SRE_Match object; span=(0, 1), match='3'>
    N None

    Process finished with exit code 0

  • 相关阅读:
    【推广】+发送短信
    【Linux】+文件操作
    【CRT】+文件上传与下载
    【Java】+查看调用关系
    【博客园】+设置
    【Java】+http
    【Postman】
    【Java】+模拟浏览器操作
    【Java】+快速打印数组
    【Java】+字符串
  • 原文地址:https://www.cnblogs.com/xwl65/p/5187508.html
Copyright © 2020-2023  润新知