I'm looking for the fastest routine (not interactively) to get the number of matches of a regexp in a string.
Something like
(count-occurrences "a" "alabama")
=> 4
count-matches
does it interactively. Maybe a good place to start looking.
how-many
(aliased count-matches
) does this, but works on buffers.
Here is one that works on strings:
(defun how-many-str (regexp str)
(loop with start = 0
for count from 0
while (string-match regexp str start)
do (setq start (match-end 0))
finally return count))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With