以这种方式使用重新查找时:
(re-find #"(\d{3})" "abc1245")我得到:
["124" "124"]当我期望只有一个价值。 这是怎么回事?
When using re-find in this way:
(re-find #"(\d{3})" "abc1245")I get:
["124" "124"]when I expect just one value. What's going on?
最满意答案
这是因为括号创建了一个正则表达式“组”。 看到
https://clojuredocs.org/clojure.core/re-find举些例子。 以下是区别:
(re-find #"(\d{3})" "abc1245") => ["124" "124"] ; #1 (re-find #"\d{3}" "abc1245") => "124" ; #2 (re-seq #"\d{3}" "abc1245") => ("124") ; #3 (re-seq #"\d{3}" "abc12345678") => ("123" "456") ; #4所以,#1给你的结果和“组结果”。 #2只给你匹配的子字符串。
#3为您提供了所有匹配的序列。 由于只有4位数字,所以剩余“5”不足以匹配3位数字。
#4总共有8位数字,所以我们得到"123"和"456"作为匹配,剩余7和8,因为我们只需要三位数字。
It is because the parentheses create a regex "group". See
https://clojuredocs.org/clojure.core/re-findfor examples. Here's the difference:
(re-find #"(\d{3})" "abc1245") => ["124" "124"] ; #1 (re-find #"\d{3}" "abc1245") => "124" ; #2 (re-seq #"\d{3}" "abc1245") => ("124") ; #3 (re-seq #"\d{3}" "abc12345678") => ("123" "456") ; #4So, #1 gives you both the result and the "group result". #2 gives you just the matched substring.
#3 gives you a sequence of all matches. Since there are only 4 digits, the remaing "5" isn't enough to match 3 digits.
#4 gives 8 digits total, so we get "123" and "456" as matches, with 7 & 8 leftover since we only want triples of digits.
更多推荐
发布评论