Files
CljElixir/stubs/Regex.clj
2026-03-09 23:09:46 -04:00

86 lines
2.4 KiB
Clojure

(ns Regex
"Elixir Regex module — regular expression operations (wraps Erlang :re).
In CljElixir: (Regex/match? ~r/pattern/ string), etc.
Regex literals use ~r/pattern/flags syntax.")
(defn compile
"Compiles a regex pattern string. Returns {:ok regex} or {:error reason}.
(Regex/compile \"^hello\") ;=> {:ok ~r/^hello/}
(Regex/compile \"hello\" \"i\") ;=> case-insensitive"
([source])
([source opts]))
(defn compile!
"Compiles a regex. Raises on invalid pattern.
(Regex/compile! \"^hello\") ;=> ~r/^hello/"
([source])
([source opts]))
(defn match?
"Returns true if `string` matches `regex`.
(Regex/match? ~r/\\d+/ \"hello123\") ;=> true"
[regex string])
(defn run
"Runs the regex against `string`. Returns list of matches or nil.
(Regex/run ~r/(\\w+)@(\\w+)/ \"user@host\") ;=> [\"user@host\" \"user\" \"host\"]
(Regex/run ~r/\\d+/ \"hello\") ;=> nil"
([regex string])
([regex string opts]))
(defn scan
"Scans the string for all matches.
(Regex/scan ~r/\\d+/ \"a1b2c3\") ;=> [[\"1\"] [\"2\"] [\"3\"]]"
([regex string])
([regex string opts]))
(defn named-captures
"Returns a map of named captures.
(Regex/named-captures ~r/(?P<year>\\d{4})-(?P<month>\\d{2})/ \"2024-03\")
;=> %{\"year\" => \"2024\", \"month\" => \"03\"}"
([regex string])
([regex string opts]))
(defn replace
"Replaces regex matches in `string` with `replacement`.
(Regex/replace ~r/\\d+/ \"a1b2\" \"X\") ;=> \"aXbX\"
(Regex/replace ~r/(\\w+)/ \"hello\" (fn [match] (String/upcase match)))"
([regex string replacement])
([regex string replacement opts]))
(defn split
"Splits `string` by `regex`.
(Regex/split ~r/\\s+/ \"hello world\") ;=> [\"hello\" \"world\"]
(Regex/split ~r/,/ \"a,b,c\" :parts 2) ;=> [\"a\" \"b,c\"]"
([regex string])
([regex string opts]))
(defn source
"Returns the source string of a compiled regex.
(Regex/source ~r/hello/) ;=> \"hello\""
[regex])
(defn opts
"Returns the options string of a compiled regex.
(Regex/opts ~r/hello/i) ;=> \"i\""
[regex])
(defn re-pattern
"Returns the underlying compiled pattern."
[regex])
(defn names
"Returns a list of named capture group names.
(Regex/names ~r/(?P<name>\\w+)/) ;=> [\"name\"]"
[regex])
(defn escape
"Escapes a string for use in a regex.
(Regex/escape \"hello.world\") ;=> \"hello\\\\.world\""
[string])
(defn regex?
"Returns true if `term` is a compiled regex."
[term])