x-lang

← Index

x/type/regex

Regular expressions with literal syntax; operations homed on the Regex class.

Syntax: #/pattern/. Supports: . * + ? \ [class] [^neg] (group) alternation ^ $ anchors {n,m} repetition \d \w \s.

Class Regex

(Regex regex? x)

Test whether a value is a regex.

Parameters:

Returns: BOOL — True if x is a regex

(Regex match str rx)

Test whether a regex matches an entire string.

Parameters:

Returns: BOOL — True if regex matches the entire string

(Regex search str rx)

Search for the first occurrence of a regex pattern in a string.

Parameters:

Returns: LIST — Pair (start end) of first match, or nil if not found

(Regex find-at str pos rx)

Search for regex starting from position pos.

Parameters:

Returns: LIST — Pair (start end) of match, or nil

(Regex find str rx)

Find first match and return the matched substring.

Parameters:

Returns: STRING — Matched substring, or nil

Examples:

(Regex find "abc123def" #/[0-9]+/) => "123"

(Regex match-groups str rx)

Capture-group texts of the FIRST match anywhere in str: an alist ((0 . whole-match) (N . group-text) …) keyed by group number in ( ) OPEN order, sorted. A group that did not participate (unmatched alternative) is ABSENT – presence door, not a sentinel; a group under a quantifier keeps its last iteration. nil when nothing matches.

Parameters:

Returns: ANY — Groups alist, or nil

Examples:

(Assoc get 2 (Regex match-groups "2026-07-19" #/([0-9]+)-([0-9]+)-([0-9]+)/)) => "07"
(Assoc get 0 (Regex match-groups "key=val" #/(\w+)=(\w+)/)) => "key=val"
(null? (Regex match-groups "nope" #/[0-9]+/)) => #t

(Regex find-all str rx)

Find all non-overlapping matches as a list of substrings.

Parameters:

Returns: LIST — List of matched substrings

Examples:

(Regex find-all "a1b22c333" #/[0-9]+/) => ("1" "22" "333")

(Regex find-all-pos str rx)

Find all non-overlapping match positions.

Parameters:

Returns: LIST — List of (start end) pairs

(Regex match-count str rx)

Count the number of non-overlapping matches. (count elsewhere means total elements; this counts MATCHES.)

Parameters:

Returns: INT — Number of non-overlapping matches

Examples:

(Regex match-count "a1b22c333" #/[0-9]+/) => 3

(Regex replace str rep rx)

Replace the first match. rep can be a string or a function that receives the matched text.

Parameters:

Returns: STRING — String with first match replaced

Examples:

(Regex replace "abc123def" "N" #/[0-9]+/) => "abcNdef"

(Regex replace-all str rep rx)

Replace all matches. rep can be a string or a function that receives each matched text.

Parameters:

Returns: STRING — String with all matches replaced

Examples:

(Regex replace-all "a1b22c333" "N" #/[0-9]+/) => "aNbNcN"

(Regex split str rx)

Split a string at regex matches.

Parameters:

Returns: LIST — List of substrings between matches

Examples:

(Regex split "a,b,c" #/,/) => ("a" "b" "c")
(#/,/ split "a,b,c") => ("a" "b" "c")

(Regex exec nodes str pos end)

Execute a regex AST against a string from the given position.

Parameters:

Returns: INT — Final position after match, or nil on failure

(Regex parse pattern)

Parse a regex pattern string into a bare AST node list. For a value the exec methods accept, use (Regex compile).

Parameters:

Returns: LIST — AST node list

(Regex compile pattern)

Compile a pattern string into a REGEX value – the programmatic twin of the #/…/ literal, for patterns built at runtime.

Parameters:

Returns: REGEX — Compiled regex, usable with every exec method and as a value-call subject

Examples:

(Regex find "abc123" (Regex compile "[0-9]+")) => "123"