Libra module

kantan.regex comes with a libra module that can be used by adding the following dependency to your build.sbt:

libraryDependencies += "com.nrinaudo" %% "kantan.regex-libra" % "0.5.3"

You then need to import the corresponding package:

import kantan.regex.libra._

And that’s pretty much it. You can now decode refined types directly.

Let’s first set our types up:

import libra._
import kantan.regex.implicits._

type Duration = QuantityOf[Int, Time, Second]

We can then simply write the following:

"[123]".evalRegex[Duration](rx"\[([+-]?\d+)\]", 1).toList
// res0: List[kantan.regex.package.DecodeResult[Duration]] = List(
//   Right(value = Quantity(value = 123))
// )

Other tutorials: