Skip to content

Regex Domain

The Regex domain handles Pattern Matching for text. Whether you're validating emails, scraping data, or searching logs, simple substring checks often aren't enough. Regex gives you a powerful, concise language to describe shapes of text. AIVI's regex support is safe (checked at compile-time with ~r/.../) and fast (compiling to native matching engines), so you don't have to worry about runtime crashes from bad patterns.

Overview

aivi
use aivi.regex (Regex)

email_pattern = ~r/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
match = Regex.test(email_pattern, "user@example.com")

// With flags (example: case-insensitive)
email_ci = ~r/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i

Types

aivi
type RegexError = InvalidPattern Text

Match = {
  full: Text,
  groups: List (Option Text),
  start: Int,
  end: Int
}

Core API (v0.1)

FunctionExplanation
compile pattern
Text -> Result RegexError Regex
Builds a Regex, returning RegexError when invalid.
test regex text
Regex -> Text -> Bool
Returns whether the regex matches anywhere in text.
match regex text
Regex -> Text -> Option Match
Returns the first Match with capture groups.
matches regex text
Regex -> Text -> List Match
Returns all matches in left-to-right order.
find regex text
Regex -> Text -> Option (Int, Int)
Returns the first match byte index range.
findAll regex text
Regex -> Text -> List (Int, Int)
Returns all match byte index ranges.
split regex text
Regex -> Text -> List Text
Splits text on regex matches.
replace regex text replacement
Regex -> Text -> Text -> Text
Replaces the first match.
replaceAll regex text replacement
Regex -> Text -> Text -> Text
Replaces all matches.

Notes:

  • match returns the first match with capture groups (if any).
  • matches returns all matches in left-to-right order.
  • replace changes the first match only; replaceAll replaces all matches.
  • Replacement strings support $1, $2, ... for capture groups.