String parse(String s) {
String str = new String(s);
final int tt_len = 4, u_len = 3;
final String tt = "tt()", u = "u()", tt_repl = "edit", u_repl = "reduce";
boolean tt_mode = false, u_mode = false;
String tmp;
for(int i = 0; i < str.length(); i++) {
tmp = str.substring(i, 5);
if(tmp.startsWith(tt)) {
tt_mode = true;
str.replaceFirst(tt, tt_repl);
i += tt_len;
if(u_mode) {
}
} else if(tmp.startsWith(u)) {
u_mode = true;
str.replaceFirst(u, u_repl);
i += u_len;
if(tt_mode) {
}
}
}
}
protected String replaceString(String aSearch, String aFind, String aReplace) {
String result = aSearch;
if (result != null && result.length() > 0) {
int a = 0;
int b = 0;
while (true) {
a = result.indexOf(aFind, b);
if (a != -1) {
result = result.substring(0, a) + aReplace + result.substring(a + aFind.length());
b = a + aReplace.length();
} else
break;
}
}
return result;
}
public static void main (String args[]){
HashMap<String,String> map = new HashMap<String, String>();
map.put("*tt()","edit");
map.put("*u()","reduce");
String sourceString = "abcd*tt()uyuy*u()yuy";
String result = replaceString(sourceString, map);
System.out.println(result);
}
public static String replaceString(String sourceString, HashMap<String,String> map){
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String patternString = shieldSpecialSymboll((String)pair.getKey());
sourceString = sourceString.replaceAll(patternString, (String)pair.getValue());
it.remove();
}
return sourceString;
}
private static String shieldSpecialSymboll(String key) {
char[] sp = {'.','^','$','*','+','?'};
for (char ch : sp){
if (key.startsWith(String.valueOf(ch)))
key = "\\"+key;
}
return key;
}
var str='abcd*tt()uyuy*u()yuy';
var length=str.length+1;
var reg1=/^[a-z0-9]*(\*tt\(\)){1}/i;
var reg2=/^[a-z0-9]*(\*u\(\)){1}/i;
var cut=0;
for (var i=1; i<length; i++) {
var temp=str.substring(cut, i);
if (reg1.test(temp)) {
console.log('edit');
cut=i;
}
if (reg2.test(temp)) {
console.log('reduce');
cut=i;
}
}