spec: regular expressions (make-regexp/regexp-match/regexp-replace + split)
Adds 9 regexp primitives to stdlib.regexp. OCaml: SxRegexp(src,flags,Re.re)
using Re.Pcre; $&/$1 capture expansion in replace. JS: native RegExp
with SxRegexp wrapper; regexp-match returns {:match :start :end :groups}.
32 tests in test-regexp.sx, all pass on both hosts.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1196,3 +1196,59 @@
|
||||
:params (s fn)
|
||||
:returns "set"
|
||||
:doc "New set of results of (fn val) for each element in s.")
|
||||
|
||||
(define-module :stdlib.regexp)
|
||||
|
||||
(define-primitive
|
||||
"make-regexp"
|
||||
:params ((pattern :as string) &rest (flags :as string))
|
||||
:returns "regexp"
|
||||
:doc "Compile regexp from pattern string and optional flags string (\"i\" case-insensitive, \"m\" multiline, \"s\" dotall).")
|
||||
|
||||
(define-primitive
|
||||
"regexp?"
|
||||
:params (v)
|
||||
:returns "boolean"
|
||||
:doc "True if v is a compiled regexp.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-source"
|
||||
:params ((re :as regexp))
|
||||
:returns "string"
|
||||
:doc "Pattern string of a regexp.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-flags"
|
||||
:params ((re :as regexp))
|
||||
:returns "string"
|
||||
:doc "Flags string of a regexp.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-match"
|
||||
:params ((re :as regexp) (str :as string))
|
||||
:returns "any"
|
||||
:doc "First match of re in str. Returns {:match \"...\" :start N :end N :groups (...)} or nil.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-match-all"
|
||||
:params ((re :as regexp) (str :as string))
|
||||
:returns "list"
|
||||
:doc "All non-overlapping matches of re in str as a list of match dicts.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-replace"
|
||||
:params ((re :as regexp) (str :as string) (replacement :as string))
|
||||
:returns "string"
|
||||
:doc "Replace first match of re in str with replacement. $& = whole match, $1..$9 = groups.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-replace-all"
|
||||
:params ((re :as regexp) (str :as string) (replacement :as string))
|
||||
:returns "string"
|
||||
:doc "Replace all matches of re in str with replacement.")
|
||||
|
||||
(define-primitive
|
||||
"regexp-split"
|
||||
:params ((re :as regexp) (str :as string))
|
||||
:returns "list"
|
||||
:doc "Split str on every match of re; returns list of strings.")
|
||||
|
||||
Reference in New Issue
Block a user