Refined module

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

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

You then need to import the corresponding package:

import kantan.regex.refined._

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

Let’s first set our types up:

import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Positive
import kantan.regex.implicits._

type PositiveInt = Int Refined Positive

We can then simply write the following:

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

And, for an error case:

"[-123]".evalRegex[PositiveInt](rx"\[([+-]?\d+)\]", 1).toList
// res1: List[kantan.regex.package.DecodeResult[PositiveInt]] = List(
//   Left(
//     value = TypeError(
//       message = "Not acceptable: 'Predicate failed: (-123 > 0).'"
//     )
//   )
// )

Other tutorials: