Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:06:27+00:00 2026-05-31T09:06:27+00:00

6.5.14 The Elvis Operator I’m seeing some trange behaviour using the elvis operator in

  • 0

6.5.14 The Elvis Operator

I’m seeing some trange behaviour using the elvis operator in SpEL. If I don’t surround the elvis expression in brackets “()” then the result of the elvis operator is returned and the rest of the expression is ignored. Sample Code showing the behaviour below:

    HashMap<String, String> facts = new HashMap<String, String>();
    facts.put("flag", "flagvalue");
    String expressionString;
    Expression expression;
    Object expressionResult;

    expressionString = "[flag]?:'' matches '(?i)flagvalue'";
    expression = new SpelExpressionParser().parseExpression(expressionString);
    expressionResult = expression.getValue(facts);
    System.out.println("Unexpected Result:" + expressionResult);

    expressionString = "([flag]?:'') matches '(?i)flagvalue'";
    expression = new SpelExpressionParser().parseExpression(expressionString);
    expressionResult = expression.getValue(facts);
    System.out.println("Expected Result:" + expressionResult);

Output:

    Unexpected Result:flagvalue
    Expected Result:true

The strange part is when the value is not in the hashmap (i.e comment the facts.put line) the elvis operator appears to work fine and both expressions return false as expected.

(using spring-framework-3.0.5)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:06:28+00:00Added an answer on May 31, 2026 at 9:06 am

    I think you need to expand your example to the Java expression to understand the difference, which would look like this:

    System.out.println(facts.containsKey("flag") ? facts.get("flag") : "".matches("(?i)flagvalue"))
    System.out.println((facts.containsKey("flag") ? facts.get("flag") : "").matches("(?i)flagvalue"))
    

    which prints

    flagvalue
    true
    

    I haven’t had a look inside the implementation, but I guess the '' matches '(?i)flagvalue' will be evaluated at first, because matches is a nested operator in the view of an expression tree.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using some F# types ( Matrix et al) from C# and so I
@lucastex posted about the Java Elvis operator, and I tried something in Scala to
I'll explain by example: Elvis Operator (?: ) The Elvis operator is a shortening
From Groovy point of view Elvis operator( ?: ) is used to return the
I working with some regular expression matching and I'm trying to figure out how
Why is the Elvis elvis definition has to be final to be used inside
I'm using one server to host multiple Node.js web apps, which are distributed across
I'm using PHP 4.3.9, Apache/2.0.52 I'm trying to get a login system working that
I'm using the Telerik RAD Controls RADEditor/WYSIWYG control as part of a Dynamic Data
I need a regular expression that matches APA format references. I currently have this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.