regex/regex
regex-compile
(regex-compile [pattern :cstr flags :int] :ptr<void>)
compile a PCRE2 pattern into a reusable regex handle.
| pattern | PCRE2 pattern string, e.g. "(?P<year>\\d{4})-(?P<month>\\d{2})" | |
| flags | option flags as :int (0 for defaults, or PCRE2_CASELESS = 0x00000008) |
result<:int> -- ok(regex-handle) on success, err(:cstr message) on failure. Free with regex-free when done.
(let [r (regex-compile "\\d+" 0)]
(when (ok? r) (regex-match (ok-val r) "abc123")))
Since: P6
regex-free
(regex-free [re :int] :void)
free a compiled regex handle.
| re | :int regex handle (unwrapped ok-val from regex-compile) |
(regex-free (ok-val r))
Since: P6
regex-match
(regex-match [re :int subject :cstr] :ptr<void>)
match a compiled regex against a subject string (first match only).
| re | :int regex handle from regex-compile | |
| subject | string to search |
result<:int> -- ok(match-handle) on match, err(:cstr) on no-match or error. Use capture-at / capture-named from regex/capture to inspect.
(let [m (regex-match re "hello 42 world")]
(when (ok? m) (capture-at (ok-val m) 0)))
Since: P6
regex-match-all
(regex-match-all [re :int subject :cstr] :int)
find all non-overlapping matches in a subject string.
| re | :int regex handle from regex-compile | |
| subject | string to search |
:int -- cons-list of match handles (may be empty / nil-value if no matches). Each element is a match handle usable with capture-at / capture-named.
(let [ms (regex-match-all re "one 1 two 2")]
(for [m ms] (println (capture-at m 0))))
Since: P6
regex-replace
(regex-replace [re :int subject :cstr replacement :cstr global :int] :ptr<void>)
replace matches of a pattern with a replacement string.
| re | :int regex handle from regex-compile | |
| subject | input string | |
| replacement | replacement string; use $1, $2 for backreferences | |
| global | if non-zero, replace all matches; otherwise replace first only |
result<:cstr> -- ok(result-string) or err(:cstr message)
(regex-replace re "hello world" "there" 0) ; replace first (regex-replace re "aaa" "b" 1) ; replace all
Since: P6
Internal definitions
__ok__err__cons