spring-bootspring-el

Spel Expression Parsing Issue with Special Characters


I am using spring Spel Expression Parser to evaluate a logical expression but it is failing when the string contain any special character

Here is the example

 private final ExpressionParser parser = new SpelExpressionParser();

 public boolean evaluate(String expressionString) {
        try {

            
            var expression = parser.parseExpression(expressionString);
            var result = expression.getValue(Boolean.class);
          
            return result;
        } catch (ParseException | SpelEvaluationException exception) {
           
            throw exception;
        }

The Input String is below :

"'Ha'il'.equalsIgnoreCase('Dubai') || 'Ha'il'.equalsIgnoreCase('Bangkok') || 'Ha'il'.equalsIgnoreCase('Phuket') || 'Ha'il'.equalsIgnoreCase('Abha')" || 'Ha'il'.equalsIgnoreCase('Ha'il')

how can it works with consideration of special characters comparison like $^'% etc


Solution

  • I believe this is explained in the docs: https://docs.spring.io/spring-framework/reference/core/expressions/language-ref/literal.html

    Strings can be delimited by single quotation marks (') or double quotation marks ("). To include a single quotation mark within a string literal enclosed in single quotation marks, use two adjacent single quotation mark characters. Similarly, to include a double quotation mark within a string literal enclosed in double quotation marks, use two adjacent double quotation mark characters.

    So, apparently your 'Ha'il' has to be like this 'Ha''il'. Or wrap it into double quotation: "Ha'il".