northisland
2016-02-26 18:37:55 +08:00
def temp(line,flag,pos,lvl=0):
if line[pos].isdigit():
flag[pos]=lvl+1
if pos+1<len(line):
return temp(line,flag,pos+1,lvl+1)
else:
return pos+1,lvl
else:
flag[pos]=0
lvl=0
return pos+1,lvl
def extract_digits(line):
"""
extract longest digit sub-str from line
:param line:
:return:
"""
flag=[0 for x in range(len(line))]
print flag
pos=0
while pos<len(flag):
pos,lvl=temp(line,flag,pos)
print flag
length=max(flag)
if length>0:
pe=flag.index(length)
return line[pe-length+1:pe+1]
else:
return ''
if __name__=='__main__':
print extract_digits('tt2012g2t11111g')