def find_nth(haystack, needle, n): start = haystack.find(needle) print "Haystack" while start >= 0 and n > 1: start = haystack.find(needle, start+len(needle)) print "start :"+str(start) print "N: " + str(n) n -= 1 return start def parseSQLFn(inSentence): sentence = inSentence numOpen = sentence.count('('); print "sentence count"+str(numOpen) ctrOpen = 0 i = 0 subSentence = sentence missingStr = 0 finSentence = '' if numOpen == 0: finSentence = inSentence return finSentence while i < numOpen*2: print 'i'+ str(i) varOpen = subSentence.find('(') varClose = subSentence.find(')') print 'VAR OPEN'+str(varOpen) print 'Var Close'+str(varClose) # Break condition 1: Open paranthesis ends if varOpen < 0: j = 0 while j < ctrOpen: # Get the End of str endOfStr = find_nth(subSentence,')',ctrOpen) print "EoS "+str(endOfStr) endOfStr+= missingStr + i + 1 print "EoS "+str(endOfStr) ctrOpen=0 finSentence = sentence[:endOfStr] # print("finSentence = " + finSentence) print "Fin String "+finSentence return finSentence if varOpen < varClose: subSentence = subSentence[varOpen+1:] print "Subsentence " + subSentence missingStr = missingStr + varOpen print "Missing str "+str(missingStr) ctrOpen+=1 elif varOpen > varClose: subSentence = subSentence[varClose+1:] print "Subsentence " + subSentence missingStr = missingStr + varClose print "Missing str "+str(missingStr) ctrOpen-=1 i=i+1 # break condition 2: All paranthesis closed but have some tail if ctrOpen == 0: print "IN CtrOpen" finSentence = sentence[:missingStr+i] print("finSentence = " + finSentence) return finSentence parseSQLFn('''to_char(sd_sdfdfc(ASS(ddnbcjdbsc_snsjdc),jdchuiadcu)))hguyuy''')
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more