Count occurrences of a substring: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add APL) |
(→{{header|Clojure}}: When using regex to search for a literal substring, remember to quote the substring) |
||
Line 724: | Line 724: | ||
Use a sequence of regexp matches to count occurrences. |
Use a sequence of regexp matches to count occurrences. |
||
<lang clojure> |
<lang clojure> |
||
(defn re-quote |
|||
"Produces a string that can be used to create a Pattern |
|||
that would match the string text as if it were a literal pattern. |
|||
Metacharacters or escape sequences in text will be given no special |
|||
meaning" |
|||
[text] |
|||
(java.util.regex.Pattern/quote text)) |
|||
(defn count-substring [txt sub] |
(defn count-substring [txt sub] |
||
(count (re-seq (re-pattern sub) txt))) |
(count (re-seq (re-pattern (re-quote sub)) txt))) |
||
</lang> |
</lang> |
||