Celixir.Ext.Regex (Celixir v0.3.0)

Copy Markdown View Source

Regex extension for CEL — mirrors ext.Regex() from cel-go.

Provides regular expression functions under the regex.* namespace. All functions are available as built-ins (no registration needed), though calling register/1 documents intent and is recommended for clarity.

Note: regex.extract returns an optional value, so optional method calls like .value() and .orValue(default) work on the result.

Usage

# Functions work without registration (built-in)
Celixir.eval!(~s|regex.replace("hello world", "hello", "hi")|)
# => "hi world"

Celixir.eval!(~s|regex.extract("item-A", "item-(\\w+)").value()|)
# => "A"

Celixir.eval!(~s|regex.extractAll("id:1, id:2", "id:\\d+")|)
# => ["id:1", "id:2"]

# Explicit opt-in (recommended)
env = Celixir.Environment.new() |> Celixir.Ext.Regex.register()

Functions

  • regex.replace(target, pattern, replacement) — replace all matches
  • regex.replace(target, pattern, replacement, count) — replace first N matches (count=0 keeps original, count<0 replaces all)
  • regex.extract(target, pattern) — optional first match (or first capture group)
  • regex.extractAll(target, pattern) — list of all matches

All functions error on invalid regex or invalid replacement string. Only \N numeric capture-group references are supported in replacements. $N style references are not supported (error).

Summary

Functions

Return optional first match (or first capture group) of pattern in target.

Return list of all matches (or first capture groups) of pattern in target.

Registers regex extension functions into the given environment.

Replace matches of pattern in target with replacement. count=-1 replaces all.

Functions

extract(target, pattern)

Return optional first match (or first capture group) of pattern in target.

extract_all(target, pattern)

Return list of all matches (or first capture groups) of pattern in target.

register(env \\ Environment.new())

Registers regex extension functions into the given environment.

Note: regex.replace cannot be registered via environment dispatch because it supports both 3-arg and 4-arg forms and Elixir anonymous functions do not support mixed arities. It is always available as a built-in.

replace(target, pattern, replacement, count \\ -1)

Replace matches of pattern in target with replacement. count=-1 replaces all.