There are a few concepts to get familiar with before getting to grips with kantan.regex proper.

Regular expression literals

First, regular expression literals: regular expressions that are validated at compile time. This is the preferred way of creating regular expressions and will be used throughout the documentation, so it’s best to get that out of the way as soon as possible.

There are various ways of enabling the feature, the simplest and most common one being to import kantan.regex.implicits._:

import kantan.regex.implicits._

This will also bring kantan.regex syntax in scope though, so if you only want the literals, you can simply import kantan.regex.literals._.

This lets you create new regular expression by prefixing string literals with rx:

// res0: java.util.regex.Pattern = \d+

rx"[ -~]"
// res1: java.util.regex.Pattern = [ -~]

And, as promised, this fails at compile time if the regular expression is not valid:

// error: Illegal regex: '[abc'
// rx"[abc"
// ^^^^^^^^

Simple evaluation

Subsequent pages will get into more details, but the simplest, most idiomatic way of extracting well typed data from strings using kantan.regex is through the evalRegex method that enriches strings (since we’ve imported kantan.regex.implicits._). For example:

"123 and some text followed by 456 and then 789".evalRegex[Int](rx"\d+").foreach(println _)
// Right(123)
// Right(456)
// Right(789)

Other tutorials: