Does doSwitch evaluate regex patterns in possibilities ?

I have a query regarding functioning of doSwitch.

I have a certain value with regard to which I want to take 2 actions depending on the pattern that value returns.

key1 → chain1,
key2 → chain2

e.g. myKey will hold a alphabetical value → abc or numerical value-> 542
And I want my keys to basically match that pattern. But doSwitch is not evaluating the regex

My Code →

“”"[a-z]+""".r → chain1,
“”"[0-9]+""".r → chain2

This is not working . But if I write the exact values as:

“”“abc”"" → chain1,
“”“542"”" → chain2
The code works.

Can anyone help me here?


The doSwitch is currently implemented with a Map. So we use direct value, not pattern matching in this case.

As you only have 2 branches, a simple doIfOrElse may work:

doIfOrElse(session => session("myKey").as[String].matches("[0-9]+")) {
} {

If you have more branches, precompute the descriminator and switch over it.


Thanks Sébastien for the reply.
I want this to work over multiple branches. Would need to precompute only.