arrow-refined-types / arrow.refinement.strings / MatchesRegex


class MatchesRegex

MatchesRegex constrains String to match a given regular expression

Safe nullable construction

import arrow.refinement.strings.MatchesRegex

MatchesRegex.Regex("hello world".toRegex()).orNull("hello world")
// hello world
MatchesRegex.Regex("hello world".toRegex()).orNull("abc")

Resolved constraints

MatchesRegex.Regex("hello world".toRegex()).constraints("hello world")
// [(true, Expected hello world to match hello world)]
MatchesRegex.Regex("hello world".toRegex()).constraints("abc")
// [(false, Expected abc to match hello world)]
MatchesRegex.Regex("hello world".toRegex()).isValid("hello world")
// true
MatchesRegex.Regex("hello world".toRegex()).isValid("abc")
// false

Folding validation

MatchesRegex.Regex("hello world".toRegex()).fold("hello world", { "failed: $it" }, { "success: $it" })
// success: MatchesRegex(value=hello world)
MatchesRegex.Regex("hello world".toRegex()).fold("abc", { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected abc to match hello world)]

Unsafe require

MatchesRegex.Regex("hello world".toRegex()).require("hello world")
// hello world
try { MatchesRegex.Regex("hello world".toRegex()).require("abc") } catch (e: IllegalArgumentException) { e.message }
// Expected abc to match hello world


Regex class Regex : Refined<String, MatchesRegex>


value val value: String

Do you like Arrow?

Arrow Org