MB
Size: a a a
MB
ST
ST
MB
MB
ST
MB
ST
MB
ST
ST
MB
ST
MB
ST
MB
(defn regex
"Given an expression that returns a string returns a function
that given a string returns nil if pattern did not match
and a map of named groups if pattern matches."
[regex-string]
(let [re-group-regex #"(?<!\\)(?:\\\\)*\((?:\?<(\w+)>|[^?])"
string-arg (gensym)
matcher-arg (gensym)
pattern (re-pattern regex-string)
group-names (mapv second (re-seq re-group-regex regex-string))
mappings (apply concat (map-indexed (fn [^long i group-name]
`(~group-name (.group ~matcher-arg ~(inc i))))
group-names))]
(eval `(fn [~string-arg]
(let [~matcher-arg (re-matcher ~pattern ~string-arg)]
(if (.matches ~matcher-arg)
{~@mappings ~@mappings}
nil))))))
MB
ST
MB
A