spring3.0学习笔记三---SpEL表达式2

例一:使用符号

//evaluates to true
boolean trueValue=parser.parseExpression("2==2").getValue(Boolean.class);
//evaluates to false
boolean falseValue=parser.parseExpression("2<-5.0").getValue(Boolean.class);

//evaluates to true
boolean trueValue=parser.parseExpression("'black'<'block'").getValue(Boolean.class); //字符串的比较

例二:判断是否是类中的一个实例

//evaluates to false
boolean falseValue=parser.parseExpression("'xyz'  instanceofT(int)").getValue(Boolean.class);

例三:和正则表达式结合

//evaluates to true
boolean trueValue = parser.parseExpression("'5.00'matches'^-?\\d+\\.\\d{2})?$'").getValue(Boolean.class);

//evaluates to false
boolean falseValue = parser.parseExpression("'5.0067'matches'^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

例四:逻辑运算符

//--AND--
//evaluates to false
boolean falseValue=parser.parseExpression("true and false").getValue(Boolean.class);//取后者
//evaluates to true
String expression= "isMember('NikolaTesla')  and  isMember('MihajloPupin')";
boolean trueValue=parser.parseExpression(expression).getValue(societyContext,Boolean.class);
//--OR--
//evaluates to  true
boolean trueValue=parser.parseExpression("true  or   false").getValue(Boolean.class);
//evaluates  to  true
String  expression= "isMember('Nikola Tesla')  or  isMember('Albert Einstien')";
boolean trueValue=parser.parseExpression(expression).getValue(societyContext,Boolean.class);
//--NOT--
//evaluates to false
boolean falseValue=parser.parseExpression(" !true").getValue(Boolean.class);
//--AND  and  NOT--
String expression= "isMember('NikolaTesla') and !isMember('MihajloPupin')";
boolean falseValue=parser.parseExpression(expression).getValue(societyContext,Boolean.class);

例五:算术运算符

//Addition
int two=parser.parseExpression("1+1").getValue(Integer.class); //2
String testString=parser.parseExpression(" 'test'+'string' ").getValue(String.class); //'teststring'
//Subtraction
int four=parser.parseExpression("1-3").getValue(Integer.class); //4
double d=parser.parseExpression("1000.00-1e4").getValue(Double.class); //-9000
//Multiplication
int six=parser.parseExpression("-2*-3").getValue(Integer.class); //6
double twentyFour=parser.parseExpression("2.0*3e0*4").getValue(Double.class); //24.0
//Division
int minusTwo=parser.parseExpression("6/-3").getValue(Integer.class); //-2
double one=parser.parseExpression("8.0/4e0/2").getValue(Double.class); //1.0
//Modulus
int three=parser.parseExpression("7%4").getValue(Integer.class); //3
int one=parser.parseExpression("8/5%2").getValue(Integer.class); //1
//Operator precedence
int minusTwentyOne=parser.parseExpression("1+2-3*8").getValue(Integer.class); //-21

 

你可能感兴趣的:(正则表达式)