wipman wipman - 1 year ago 117
Scala Question

Match among values of List

I want to write some clean


I have a

val myList = List(

I would like to do something approaching the following:

myInputString match {
case matchAnyRegexFromMyList(s) => //do something

As an alternative to:

myInputString match {
case regex1(a)|regex2(b)... => //do something

Is it at least possible to be more concise?

Answer Source

If your patterns were not pre-compiled you could combine them when they are compiled.

val myList = List( ".+\\.a"
                 , ".+\\.b"
                 , ".+\\.c" )

val myListRE = myList.mkString("|").r

"blah.b" match {
  case myListRE() => println("hit")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download