Minimal, safe Regex support for PTC-Lisp. Uses Erlang's :re directly with match limits for ReDoS protection.
Summary
Functions
Extract a capture group from a regex match.
Extract a capture group and parse as integer.
Find first match of regex in string. Returns string if no groups, or vector of [full match, group1, ...] if groups.
Returns match if regex matches the entire string.
Compile a string into a regex. Returns opaque {:re_mp, mp, anchored_mp, source} tuple. Both normal and anchored versions are pre-compiled for performance and safety.
Find all matches of regex in string. Returns list of matches (empty list if no matches).
Split string by regex pattern. Returns list of substrings.
Functions
Extract a capture group from a regex match.
(extract "ID:(\d+)" "ID:42")=> "42" (group 1)(extract "ID:(\d+)" "ID:42" 0)=> "ID:42" (full match)(extract regex string 2)=> group 2
Accepts both string patterns and compiled regex objects.
Extract a capture group and parse as integer.
2-arity: extracts group 1, returns nil on failure
(extract-int "age=(\d+)" "age=25")=> 25
4-arity: extracts specified group with default value
(extract-int "age=(\d+)" "no match" 1 0)=> 0 (group 1, default 0)(extract-int "x=(\d+) y=(\d+)" s 2 0)=> group 2 with default 0
Accepts both string patterns and compiled regex objects.
Find first match of regex in string. Returns string if no groups, or vector of [full match, group1, ...] if groups.
Returns match if regex matches the entire string.
Compile a string into a regex. Returns opaque {:re_mp, mp, anchored_mp, source} tuple. Both normal and anchored versions are pre-compiled for performance and safety.
Find all matches of regex in string. Returns list of matches (empty list if no matches).
Examples
(re-seq (re-pattern "\d+") "a1b2c3") => ["1" "2" "3"]
(re-seq (re-pattern "(\d)(\w)") "1a2b") => [["1a" "1" "a"] ["2b" "2" "b"]]
Split string by regex pattern. Returns list of substrings.
Examples
(re-split (re-pattern "\s+") "a b c") => ["a" "b" "c"]
(re-split (re-pattern ",") "a,b,c") => ["a" "b" "c"]