簡(jiǎn)單點(diǎn)就是依據(jù)括號(hào)了吴超。
/**
* @author eric
* @date 2021/6/30
*/
public class OgnlCheckUtil {
/**
* 左括號(hào) 1鸯乃,右括號(hào) -1跋涣,最后總和0則匹配
*
* @param ognl
* @return
*/
public static void check(String ognl) {
int top = 0;
char[] express = ognl.toCharArray();
for (int i = 0; i < express.length; i++) {
if (express[i] == '(') {
top++;
} else if (express[i] == ')') {
if (top > 0) {
top--;
} else {
//top <= 0,無(wú)左括號(hào)卻有一個(gè)右括號(hào),有一個(gè)右括號(hào)不匹配
throw new RuntimeException("多了)");
}
}
}
if (top > 0) {
throw new RuntimeException("多了(");
} else if (top < 0) {
throw new RuntimeException("多了)");
}
}
}