也许我说这一切都错了,但我正在尝试将字符串中的所有匹配项用于特定的正则表达式模式.我正在使用re-matcherMatch对象,我传递给它re-find,给我(full-string-match,grouped-text)对.我如何得到Match对象产生的所有匹配序列?
在Clojuresque Python中,它看起来像:
pairs = []
match = re-matcher(regex, line)
while True:
pair = re-find(match)
if not pair: break
pairs.append(pair)
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Ale*_*ard 23
您可能希望使用内置的re-seq和Clojure内置的正则表达式文字.除非你真的有,否则不要乱用底层的java对象.
(doc re-seq)
Run Code Online (Sandbox Code Playgroud)
clojure.core/re-seq
([re s])
Returns a lazy sequence of successive matches of pattern in string,
using java.util.regex.Matcher.find(), each such match processed with
re-groups.
For example:
user> (re-seq #"the \w+" "the cat sat on the mat")
("the cat" "the mat")
Run Code Online (Sandbox Code Playgroud)
In answer to the follow-up comment, group captures will result in a vector of strings with an element for each part of the group in a match:
user> (re-seq #"the (\w+(t))" "the cat sat on the mat")
(["the cat" "cat" "t"] ["the mat" "mat" "t"])
Run Code Online (Sandbox Code Playgroud)
You can extract a specific element by taking advantage of the elegant fact that vectors are functions of their indices.
user> (defn extract-group [n] (fn [group] (group n)))
#'user/extract-group
user> (let [matches (re-seq #"the (\w+(t))" "the cat sat on the mat")]
(map (extract-group 1) matches))
("cat" "mat")
Run Code Online (Sandbox Code Playgroud)
Or you can destructure the matches (here using a for宏来遍历所有匹配,但这也可以在一个let或函数参数绑定中完成):
user> (dorun
(for [[m1 m2 m3] (re-seq #"the (\w+(t))" "the cat sat on the mat")]
(do (println "m1:" m1)
(println "m2:" m2)
(println "m3:" m3))))
m1: the cat
m2: cat
m3: t
m1: the mat
m2: mat
m3: t
Run Code Online (Sandbox Code Playgroud)