regex/regex
Regex
(Regex)
compiled PCRE2 pattern handle.
Since: 0.2.0
Match
(Match)
result of a successful match against a Regex.
Since: 0.2.0
regex-compile
(regex-compile [pattern : cstr flags : int] :)
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 Regex cstr) -- ok(Regex) on success, err(msg) on parse failure. Free the Regex with regex-free when done.
(let [r (regex-compile "\\d+" 0)]
(when (ok? r) (regex-match (ok-val r) "abc123")))
Since: P6 (typed as Result in 0.2.0)
regex-free
(regex-free [re : Regex] :)
free a compiled Regex handle.
| re | Regex handle (unwrapped ok-val from regex-compile) |
(regex-free (ok-val r))
Since: P6 (typed as Regex in 0.2.0)
regex-match
(regex-match [re : Regex subject : cstr] :)
match a compiled Regex against a subject string (first match only).
| re | Regex handle from regex-compile | |
| subject | string to search |
(Result Match cstr) -- ok(Match) on match, err(msg) 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 (typed in 0.2.0)
regex-match-all
(regex-match-all [re : Regex subject : cstr] :)
find all non-overlapping matches in a subject string.
| re | Regex handle from regex-compile | |
| subject | string to search |
:int -- cons-list of Match handles (may be empty / nil if no matches). Each element is a Match 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 (typed in 0.2.0)
regex-replace
(regex-replace [re : Regex subject : cstr replacement : cstr global : int] :)
replace matches of a pattern with a replacement string.
| re | 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 cstr) -- ok(result-string) or err(message)
(regex-replace re "hello world" "there" 0) ; replace first (regex-replace re "aaa" "b" 1) ; replace all
Since: P6 (typed in 0.2.0)
regex-of
(regex-of [r ptr<void>] :)
extract a typed Regex handle from an ok result returned by regex-compile.
match-of
(match-of [r ptr<void>] :)
extract a typed Match handle from an ok result returned by regex-match.