zoukankan      html  css  js  c++  java
  • python re 正则匹配 split sub

    import re

    编译:

    motif=‘([ST])Q’

    seq="SQAAAATQ"

    regrex=re.compile(motif) #编译成正则对象

    regrex=re.compile(motif,re.IGNORECASE) #编译成正则对象,忽略大小写

    匹配:

    sea=regrex..search(seq) #返回第一次的匹配对象

    mat=regrex.match(seq)#从序列开始位置寻找正则匹配对象

    all=regrex.findall(seq)#返回包含所有匹配的子字符串的列表 。如果匹配有两个以上的有括号()group,返回的是由group元组的列表 [(group1,group2),(...)],每个元组是一个匹配项 。如果只有一个括号组,则返回匹配括号组成的列表见例子

    ier=regrx.finditer(seq)#返回所有匹配对象的迭代器

     for i in iter:

         print i.group()  #group()返回匹配对象字符串内容 

        print i.group(1) #返回匹配到的子组

         print i.span()  #返回匹配对象的包含对象的元组

         print i.start()  #返回匹配对象的起始位置

         print i.end()  #返回匹配对象的终止位置

    匹配到后修改字符串

    1.split字符串

    separator=re.compile('|')

    anno=''A|B|C"

    col=separator.split(anno)

    2.替换内容

    new=separator.sub("@",anno) # sub(r,s,[c]) 将s中匹配到的前c个'|'替换成@,默认全部替换

    sublist=separator.subn("@",anno) #subn(r,s,[c]) 返回元组 (新的字符串,替代的数量)

    finall 2个以上grop例子:

      str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'
      tuples = re.findall(r'([w.-]+)@([w.-]+)', str)
      print tuples  ## [('alice', 'google.com'), ('bob', 'abc.com')]
      for tuple in tuples:
        print tuple[0]  ## username
        print tuple[1]  ## host

    输出:

    [('alice', 'google.com'), ('bob', 'abc.com')]
    alice
    google.com
    bob
    abc.com
    

      

    findall 1个括号组 

    import re
    str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'
    tuples = re.findall(r'([w.-]+)@[w.-]+', str)
    print tuples  ## 
    for tuple in tuples:
        print tuple[0]  ## username
        print tuple[1]  ## host
    

    输出: 

    ['alice', 'bob']
    a
    l
    b
    o
    

    忽视大小写:

    re.search(pat, str, re.IGNORECASE) 

    soapnuke 例子:

    p = re.compile('S+_(1|2).(fq|fastq).gz')
    if p.search(i):
        key = 'fq' + str(p.search(i).group(1))
        fqs[key] = i
    

      

    本文来自博客园,作者:BioinformaticsMaster,转载请注明原文链接:https://www.cnblogs.com/koujiaodahan/p/8068272.html

  • 相关阅读:
    react的50个面试题
    什么是宏队列跟微队列
    宏队列与微队列
    数组都有哪些方法
    vuex 跟 vue属性
    高阶组件
    如何创建视图簇(View cluster)-SE54/SM34
    ◆◆0如何从维护视图(Maintenace view)中取数据-[VIEW_GET_DATA]
    ◆◆0如何在SM30维护表时自动写入表字段的默认值-事件(EVENT)
    ◆◆0SAP Query 操作教程
  • 原文地址:https://www.cnblogs.com/koujiaodahan/p/8068272.html
Copyright © 2011-2022 走看看