re

Regular expressions backed by the RE2 engine, guaranteeing linear-time matching against any input with no catastrophic backtracking. The cost is that some PCRE features are unavailable — notably backreferences and lookbehind. Provides pattern compilation; match, find, find-all, and lazy find-iter; literal and function-based replace; split; named and numbered capture groups; and pattern inspection.

re:compile

[$re:compile] -> element — Compile a pattern string into an opaque reusable regex value using default flags.

            [?lib 'cx-stdlib/re']
[$re:pattern-text [$re:compile "a.b"]]
          
            'a.b'
          

re:compile-with-flags

[$re:compile-with-flags] -> element — Compile a pattern with a flags map (case-insensitive, multiline, dotall, unicode, literal, match-byte limit).

            [?lib 'cx-stdlib/re']
[$re:matches [$re:compile-with-flags "abc" {"case-insensitive": true}] "ABC"]
          
            true
          

re:matches

[$re:matches] -> bool — Report whether the pattern matches the entire string.

            [?lib 'cx-stdlib/re']
[$re:matches [$re:compile "a+"] "aaa"]
          
            true
          

re:find

[$re:find] -> element — Find the first match, returning a match element with start, end, text, and groups — or [no-match].

            [?lib 'cx-stdlib/re']
[$re:group [$re:find [$re:compile "[0-9]+"] "abc123def"] 0]
          
            '123'
          

re:find-from

[$re:find-from] -> element — Find the first match at or after the given byte position.

            [?lib 'cx-stdlib/re']
[$re:group [$re:find-from [$re:compile "ab"] "abab" 2] 0]
          
            'ab'
          

re:find-all

[$re:find-all] -> [sequence element] — Find all non-overlapping matches as a materialized sequence of match elements.

            [?lib 'cx-stdlib/re']
[$count [$re:find-all [$re:compile "ab"] "abXabYab"]]
          
            3
          

re:find-iter

[$re:find-iter] -> [iterator element] — Lazily iterate over non-overlapping matches, yielding each match element on demand.

            [?lib 'cx-stdlib/re']
[$count [$re:find-iter [$re:compile "ab"] "abXabYab"]]
          
            3
          

re:group

[$re:group] -> string — Return the text of numbered capture group i in a match, where 0 is the full match.

            [?lib 'cx-stdlib/re']
[$re:group [$re:find [$re:compile "[0-9]+"] "abc123def"] 0]
          
            '123'
          

re:group-named

[$re:group-named] -> string — Return the text of a named capture group in a match.

            [?lib 'cx-stdlib/re']
[$re:group-named [$re:find [$re:compile "(?P[0-9]{4})"] "y2026"] "year"]
          
            '2026'
          

re:groups-all

[$re:groups-all] -> [sequence string] — Return all capture groups of a match as a sequence, starting with the full match.

            [?lib 'cx-stdlib/re']
[$re:groups-all [$re:find [$re:compile "(a+)(b+)"] "aabbb"]]
          
            ('aabbb', 'aa', 'bbb')
          

re:groups-map

[$re:groups-map] -> map — Return the named capture groups of a match as a name-to-text map.

            [?lib 'cx-stdlib/re']
[$map-get [$re:groups-map [$re:find [$re:compile "(?P[0-9]{4})"] "y2026"]] "year"]
          
            '2026'
          

re:replace

[$re:replace] -> string — Replace all matches using a template string with $0, $1, ${name}, and $$ substitutions.

            [?lib 'cx-stdlib/re']
[$re:replace [$re:compile "[0-9]+"] "a1b22c333" "<$0>"]
          
            'a<1>b<22>c<333>'
          

re:replace-first

[$re:replace-first] -> string — Replace only the first match using a template string.

            [?lib 'cx-stdlib/re']
[$re:replace-first [$re:compile "[0-9]+"] "a1b2c3" "<$0>"]
          
            'a<1>b2c3'
          

re:replace-fn

[$re:replace-fn] -> string — Replace each match with the result of a callable invoked on that match element.

            [?lib 'cx-stdlib/re']
[$re:replace-fn [$re:compile "[0-9]+"] "a1b22c" [?fn ($m) [$re:group $m 0]]]
          
            'a1b22c'
          

re:split

[$re:split] -> [sequence string] — Split a string into segments around every match of the pattern.

            [?lib 'cx-stdlib/re']
[$re:split [$re:compile ","] "a,b,c"]
          
            ('a', 'b', 'c')
          

re:split-limit

[$re:split-limit] -> [sequence string] — Split a string on the pattern into at most max+1 segments.

            [?lib 'cx-stdlib/re']
[$re:split-limit [$re:compile ","] "a,b,c,d" 1]
          
            ('a', 'b,c,d')
          

re:group-count

[$re:group-count] -> int — Return the number of capture groups in a compiled pattern.

            [?lib 'cx-stdlib/re']
[$re:group-count [$re:compile "(a)(b)"]]
          
            2
          

re:group-names

[$re:group-names] -> [sequence string] — Return the names of the named capture groups in a compiled pattern.

            [?lib 'cx-stdlib/re']
[$re:group-names [$re:compile "(?P[0-9]{4})-(?P[0-9]{2})"]]
          
            ('year', 'month')
          

re:pattern-text

[$re:pattern-text] -> string — Return the original source pattern string of a compiled regex.

            [?lib 'cx-stdlib/re']
[$re:pattern-text [$re:compile "a.b"]]
          
            'a.b'
          

re:pattern-flags

[$re:pattern-flags] -> map — Return the resolved compilation flags of a compiled regex as a map.

            [?lib 'cx-stdlib/re']
[$map-get [$re:pattern-flags [$re:compile-with-flags "abc" {"case-insensitive": true}]] "case-insensitive"]
          
            true
          

re:escape

[$re:escape] -> string — Escape regex metacharacters in a string (RE2 QuoteMeta) so it matches as a verbatim literal.

            [?lib 'cx-stdlib/re']
[$re:matches [$re:compile [$re:escape "a.b*c"]] "a.b*c"]
          
            true