擅长:python、mysql、java
<p>Clojure/Java更典型的做法是使用起始字符的索引和结束字符后的索引,因此使用<code>[0, 5]</code>和<code>[13, 18]</code>。Java的<a href="http://docs.oracle.com/javase/tutorial/essential/regex/matcher.html" rel="nofollow noreferrer">Matcher</a>将以这种方式返回每个匹配的开始和结束。你知道吗</p>
<pre class="lang-clj prettyprint-override"><code>(def strg "apple orange apple")
(defn re-indices [re s]
(let [m (re-matcher re s)]
((fn step []
(when (. m find)
(cons [(. m start) (. m end)] (lazy-seq (step))))))))
(re-indices #"\S+" strg)
;=> ([0 5] [6 12] [13 18])
</code></pre>
<p>而且<code>subs</code>会适当地使用它们</p>
<pre class="lang-clj prettyprint-override"><code>(->> (re-indices #"\S+" strg)
(group-by (partial apply subs strg)))
;=> {"apple" [[0 5] [13 18]], "orange" [[6 12]]}
</code></pre>
<p>从这里您可以过滤出只有那些子串键与一个以上的索引对。你知道吗</p>