鉴于:
String str = (a AND b) OR (c AND d);
String[] tokened = [a, b, c, d]
String[] edited = [a=1, b=1, c=1, d=1]
简单地:
for (int i=0; i<tokened.length; i++)
str.replaceAll(tokened[i], edited[i]);
编辑:
String addstr = "=1";
String str = "(a AND b) OR (c AND d) ";
String delims = "AND|OR|NOT|[!&|() ]+"; // Regular expression syntax
String[] tokens = str.trim().split( delims );
String[] delimiters = str.trim().split( "[a-z]+"); //remove all lower case (these are the characters you wish to edit)
String newstr = "";
for (int i = 0; i < delimiters.length-1; i++)
newstr += delimiters[i] + tokens[i] + addstr;
newstr += delimiters[delimiters.length-1];
好的,现在解释:
tokens = [a, b, c, d]
delimiters = [ "(" , " AND " , ") OR (" , " AND " , ") " ]
当遍历分隔符时,我们采用“(”+“a”+“=1”。
从那里我们有"(a=1" += " AND " + "b" + "=1"。
然后:“(a=1 AND b=1” +=“)或(”+“c”+“=1”。
再次:“(a=1 AND b=1) OR (c=1" += " AND " + "d" + "=1"
最后(在for
循环外):“(a=1 AND b=1) OR (c=1 AND d=1" += ")"
我们有:“(a=1 AND b=1) OR (c=1 AND d=1)”