propensive / kaleidoscope

Compile-time-checked string pattern matching for Scala


Latest version Maven Central


Join the chat at


An early release of Kaleidoscope is available on Maven Central, and can be included in an sbt project by including,

"com.propensive" %% "kaleidoscope" % "0.1.0"

in your build.sbt file.


To use Kaleidoscope, first import its package,

import kaleidoscope._

and you can then use a Kaleidoscope regular expression—a string prefixed with the letter r—anywhere you can use a pattern in Scala. For example,

path match {
  case r"/images/.*" => println("image")
  case r"/styles/.*" => println("stylesheet")
  case _ => println("something else")


email match {
  case r"^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$$" => Some(email)
  case _ => None

Such patterns with either match or not, however should they match, it is possible to extract parts of the matched string using capturing groups. The pattern syntax is exactly as described in the Java Standard Library, with the exception that a capturing group (enclosed within ( and )) may be bound to an identifier by prefixing the group with an @, and the identifier to extract to, which in standard Scala syntax, is written either as $identifier or ${identifier}.

Here is an example:

path match {
  case r"/images/${img}@(.*)" => Image(img)
  case r"/styles/$styles@(.*)" => Stylesheet(styles)

or as an extractor on a val, like so,

val r"^[a-z0-9._%+-]+@$domain@([a-z0-9.-]+\.$tld@([a-z]{2,6})$$" = ""
> domain: String = ""
> tld: String = "com"

In addition, regular expressions will be checked at compile-time, and any issues will be reported then.


Note that inside an extractor pattern string, whether it is single- (r"...") or triple-quoted (r"""..."""), special characters, notably \, do not need to be escaped, with the exception of $ which should be written as $$. It is still necessary, however, to follow the regular expression escaping rules, for example, an extractor matching a single opening parenthesis would be written as r"\(" or r"""\(""".


Kaleidoscope currently has no support for optional or repeated capturing groups, however this will probably be added very soon.


This is very experimental software, and should not be considered production-ready.


There is currently no website for Kaleidoscope.


Kaleidoscope is made available under the Apache 2.0 license.