1. // 迭代删除 ()...()...()
def parseTail(sqlContext: String): String = { val pre = sqlContext.indexOf("(") val end = sqlContext.indexOf(")") if(pre == -1 && end == -1) return sqlContext parseTail(sqlContext.substring(0, pre) + sqlContext.substring(end + 1)) } parseTail(sql)
2. // 迭代删除 (..(..)..).. (..) n重嵌套结构
// 迭代计算 ( 个数 def parseIndexNum(s: String): Int = { if (s.indexOf("(") > s.indexOf(")") || s.indexOf("(") == -1) return 0 return parseIndexNum(s.substring(s.indexOf("(") + 1)) + 1 } // 迭代删除 ()...()...() def parseTail(sqlContext: String): String = { val pre = sqlContext.indexOf("(") var end = sqlContext.indexOf(")") if (pre == -1 || end == -1) return sqlContext var sqlCopy = sqlContext val pre_count = parseIndexNum(sqlContext) if (pre_count > 1) { (0 until pre_count - 1).foreach(_ => { val index = sqlCopy.indexOf(")") sqlCopy = sqlCopy.substring(0, index) + "w" + sqlCopy.substring(index + 1) }) end = sqlCopy.indexOf(")") } parseTail(sqlContext.substring(0, pre) + sqlContext.substring(end + 1)) }