1 """ Python RegularExpression example and
2 answer of http://bioinfo.sarang.net/technote/read.cgi?board=QnAboard&nnew=2&y_number=199
3 """
4 import unittest, re
5
6 instr=">consensus:MG-U74Av2:99494_at; gb|AJ001700; Cluster Incl AJ001700:Mus musculus mRNA for neuroserpin /cds=(97,1329) /gb=AJ001700 /gi=2462594 /ug=Mm.41560 /len=2944"
7
8 ug="Mm\.\d+"
9
10 def getUg(anInStr):
11 m = re.compile(ug)
12 p = m.search(anInStr)
13 if p:
14 return p.group()
15
16 def getUg2(anInStr):
17 return re.search(ug,anInStr).group()
18
19 class ReTest(unittest.TestCase):
20 def testGetUg(self):
21 self.assertEquals('Mm.41560', getUg(instr))
22 def testGetUg2(self):
23 self.assertEquals('Mm.41560', getUg2(instr))
24
25
26
27 def getTraceIds(besid):
28 url_bes="http://xx.xx.xx/cgi-bin/Projects/S_scrofa/WebFPCreport.cgi?mode=wfcreport&name=%s"
29 p_tid = re.compile(r'\?traceid=(\w+)\"')
30 result=list()
31 doc=urllib2.urlopen(url_bes%besid).read()
32 while True:
33 m=p_tid.search(doc)
34 if not m: break
35 result.append(m.group(1))
36 doc=doc[m.span()[1]:]
37 return result
38
39 if __name__=='__main__':
40 unittest.main(argv=('','-v'))