字符串如下:
(sum(UtranCell_R1_06_00:UC1054_Cum) + sum(UtranCell_R1_06_00:UC2686)) * (1+(sum(UtranCell_R1_06_00:UC1046) + sum(RncFunction_R8805_01:UC1049_Avg)) / (sum(RncFunction_R8805_01:UC934_Cum) + sum(RncFunction_R8805_01:UC934_Min)))要求:
取出所有 sum( 的索引位置,并取出所有sum后面括弧中的内容。折腾好久了。望高手帮忙写写。3Q
(sum(UtranCell_R1_06_00:UC1054_Cum) + sum(UtranCell_R1_06_00:UC2686)) * (1+(sum(UtranCell_R1_06_00:UC1046) + sum(RncFunction_R8805_01:UC1049_Avg)) / (sum(RncFunction_R8805_01:UC934_Cum) + sum(RncFunction_R8805_01:UC934_Min)))要求:
取出所有 sum( 的索引位置,并取出所有sum后面括弧中的内容。折腾好久了。望高手帮忙写写。3Q
要求:
取出所有 sum( 的索引位置,并取出所有sum后面括弧中的内容。没看懂,举个例子
public static void main(String[] args) {
String inputStr = "(sum(UtranCell_R1_06_00:UC1054_Cum) + sum(UtranCell_R1_06_00:UC2686)) * (1+(sum(UtranCell_R1_06_00:UC1046) + sum(RncFunction_R8805_01:UC1049_Avg)) / (sum(RncFunction_R8805_01:UC934_Cum) + sum(RncFunction_R8805_01:UC934_Min)))";
String patternStr = "sum\\((.*?)\\)";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
while(matchFound) {
System.out.println(matcher.start() + "-" + matcher.group(1));
if(matcher.end() + 1 <= inputStr.length()) {
matchFound = matcher.find(matcher.end());
}else{
break;
}
}
String[] sum = str.split("sum");
String tmp = "";
for(int i=0;i<sum.length;i++){
tmp = sum[i];
int index = tmp.indexOf(")");
if(index > 0){
tmp = tmp.substring(0, index+1);
System.err.println(tmp);
}
}
楼主看下这个:
package soduku;import java.util.Vector;public class Find {
private Vector<Match> matchs;
private Vector<Match> finding;
private String string;
public Find(String string) {
this.string = string;
matchs = new Vector<Match>(0);
finding = new Vector<Match>(0);
}
/**
* 寻找括号匹配
*/
public void checkMatchs() {
matchs.clear();
finding.clear();
int start = 0;
int end = string.length();
Match match;
char ch;
while (start < end) {
ch = string.charAt(start);
if (ch == '(') {
match = new Match(start);
finding.add(match);
} else if (ch == ')') {
if (finding.size() > 0) {
finding.lastElement().setMatchPos(start);
matchs.add(finding.lastElement());
finding.remove(finding.lastElement());
} else {
System.out.println("警告:有无法匹配的括号!");
}
}
start++;
}
}
private int checkMatch(int pos) {
for (Match mc : matchs) {
if (mc.getPos() == pos) {
return mc.getMatchPos();
}
}
return -1;
}
public void showResult() {
int start = 0;
int end = string.length();
while (start < end) {
int sum = string.indexOf("sum(", start);
if (sum > 0) {
int sumend = checkMatch(sum+3);
if (sumend >= 0) {
System.out.println("index " + sum + " : " +
string.substring(sum+4, sumend));
}
start = sum+4;
} else {
start = end;
}
}
}
class Match {
private int pos;
private int matchPos;
public Match(int pos) {
this.setPos(pos);
} public void setMatchPos(int matchPos) {
this.matchPos = matchPos;
} public int getMatchPos() {
return matchPos;
} public void setPos(int pos) {
this.pos = pos;
} public int getPos() {
return pos;
}
}
public static void main(String arg[]) {
Find find = new Find("(sum(UtranCell_R1_06_00:UC1054_Cum) + sum(UtranCell_R1_06_00:UC2686)) * (1+(sum(UtranCell_R1_06_00:UC1046) + sum(RncFunction_R8805_01:UC1049_Avg)) / (sum(RncFunction_R8805_01:UC934_Cum) + sum(RncFunction_R8805_01:UC934_Min)))");
find.checkMatchs();
find.showResult();
}}
public static void main(String[] args) {
String str = "(sum(UtranCell_R1_06_00:UC1054_Cum) + " + "" +
"sum(UtranCell_R1_06_00:UC2686)) * (1+" +
"(sum(UtranCell_R1_06_00:UC1046) + " +
"sum(RncFunction_R8805_01:UC1049_Avg)) / " +
"(sum(RncFunction_R8805_01:UC934_Cum) + " +
"sum(RncFunction_R8805_01:UC934_Min)))";
find(str);
}
private static void find(String str) {
int start = 0,end = 0;//"sum("的位置和")"的位置
String temp = "";
while(end < str.length()) {
start = str.indexOf("sum(", start);
if(start == -1) {//如果没有找到"sum{",就结束
break;
}
end = str.indexOf(")", start);
temp = str.substring(start, end);
System.out.println(start + ":" + temp);
start = end + 1;//开始位置为上一次的结束位置加1
}
}
}
public Testmain() {
String str = "(sum(UtranCell_R1_06_00:UC1054_Cum) + sum(UtranCell_R1_06_00:UC2686)) * (1+(sum(UtranCell_R1_06_00:UC1046) + sum(RncFunction_R8805_01:UC1049_Avg)) / (sum(RncFunction_R8805_01:UC934_Cum) + sum(RncFunction_R8805_01:UC934_Min)))";
//定义sum(的位置
int t = 0;
//定义sum(后面括号中的字符串。
String s = null;
while(t<str.length()){
t = str.indexOf("sum(",t);
if(t == -1){
break;
}
System.out.println(t);
t += 4;
s = str.substring(t, str.indexOf(")",t));
System.out.println(s);
}
}
public static void main(String[] args) {
new Testmain();
}}
south_abc=上面字符串那一长串其实这是个公式,=左边是一张表里的字段,字段值=上面的字符串计算的结果。其中,sum即是mysql中的sum()函数,括弧中:前是表名,:后是字段名。
也就是这是跨表计算。
大概思路是循环那些表,主键值相等时,取出那些表相应的字段值,进行算术计算。我需要找出sum函数所在字符串中的位置
括弧中的内容
然后挨个计算出sum()值是多少
然后用这些值替换掉原字符串中原来的字符所在处。
因为这是个比较复杂的算术计算。