tur/re
POSIX extended regular expressions via regcomp/regexec.
Since: Phase B1
re/compile
(re/compile [pattern :cstr] :int)
compile a regular expression pattern.
| pattern | NUL-terminated regex pattern cstr (POSIX extended syntax) |
An opaque handle (:int) on success, or 0 on compilation error. Free with re/free when done.
(re/compile "([0-9]+)") ; => <handle>
Since: Phase B2
re/free
(re/free [re :int] :void)
release a compiled regex handle.
| re | handle returned by re/compile |
(re/free my-re)
Since: Phase B2
re/match?
(re/match? [re :int input :cstr] :bool)
test whether a compiled pattern matches anywhere in input.
| re | compiled regex handle from re/compile | |
| input | input string cstr |
true if the pattern matches, false otherwise.
(re/match? my-re "abc123") ; => true
Since: Phase B2
re/match
(re/match [re :int input :cstr] :int)
match a regex against input and return capture groups as a vec.
| re | compiled regex handle from re/compile | |
| input | input string cstr |
A vec pointer (:int) whose elements are cstr pointers on success, or 0 if there is no match. Free with re/match-free.
(re/match my-re "abc123") ; => vec of capture group cstrs
Since: Phase B2
re/match-free
(re/match-free [v :int] :void)
free a vec returned by re/match.
| v | vec pointer returned by re/match (may be 0) |
(re/match-free my-match)
Since: Phase B2
re/find-all
(re/find-all [re :int input :cstr] :int)
find all non-overlapping matches of a regex in input.
| re | compiled regex handle from re/compile | |
| input | input string cstr |
A cons list of vec pointers (each vec as returned by re/match). Returns 0 (empty list) if there are no matches. Free with re/find-all-free.
(re/find-all my-re "a1b2c3") ; => cons list of match vecs
Since: Phase B2
re/find-all-free
(re/find-all-free [lst :int] :void)
free a cons list returned by re/find-all.
| lst | cons list of match vecs returned by re/find-all |
(re/find-all-free my-matches)
Since: Phase B2
re/replace
(re/replace [re :int input :cstr replacement :cstr] :cstr)
replace the first match of a regex in input.
| re | compiled regex handle from re/compile | |
| input | input string cstr | |
| replacement | replacement string cstr (literal; no backreferences) |
A heap-allocated cstr with the first match replaced, or a strdup of input if there is no match. The caller must free the result.
(re/replace my-re "hello world" "WORLD") ; => "hello WORLD"
Since: Phase B2
re/union-patterns
(re/union-patterns [patterns :int] :cstr)
build an alternation regex from a cons list of patterns.
| patterns | cons list of cstr patterns (head = cstr pointer, tail = next) |
A heap-allocated cstr containing the unioned pattern, or 0 if the list is empty or contains a null element. POSIX ERE has no portable "match nothing" pattern, so empty input is reported as 0 rather than silently producing a degenerate regex.
(re/union-patterns (cons "[A-Za-z]+" (cons "[0-9]+" 0)))
; => "([A-Za-z]+)|([0-9]+)"
Since: Phase RU1
re/compile-union
(re/compile-union [patterns :int] :int)
compile an alternation of multiple regex patterns.
| patterns | cons list of cstr patterns (head = cstr pointer, tail = next) |
A compiled regex handle (:int) on success, or 0 if the input list is empty, contains a null element, or compilation fails.
(re/compile-union (cons "[A-Za-z]+" (cons "[0-9]+" 0))) ; => <handle>
Since: Phase RU2
re/replace-all
(re/replace-all [re :int input :cstr replacement :cstr] :cstr)
replace all non-overlapping matches of a regex.
| re | compiled regex handle from re/compile | |
| input | input string cstr | |
| replacement | replacement string cstr (literal; no backreferences) |
A heap-allocated cstr with all matches replaced. Caller must free.
(re/replace-all my-re "a1b2c3" "X") ; => "aXbXcX"
Since: Phase B2